-2
我也跟着上youtube教程ABT RecyclerView
,並寫了沒有錯誤的運行,但未按預期的任何幫助,請這方面我的代碼RecyclerView運行良好,但沒有結果
XML顯示列表的代碼和適配器
custom_list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/title_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dummy Text"
android:paddingTop="5dp"
android:paddingLeft="15dp"
android:textColor="#ff281c9b" />
</LinearLayout>
vanguard_news.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="56dp">
<android.support.v7.widget.RecyclerView
android:id="@+id/feed_list"
android:layout_height="match_parent"
android:layout_width="wrap_content">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
類代碼
ViewAdapter.class
public class ViewAdapter extends RecyclerView.Adapter <ViewAdapter.MyViewHolder>{
LayoutInflater inflater;
List<ReViewInformation> data= Collections.emptyList();
public ViewAdapter(Context context, List<ReViewInformation> data){
inflater=LayoutInflater.from(context);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.custom_list_row,parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
ReViewInformation currObj = data.get(position);
holder.title.setText(currObj.Title);
holder.image.setImageResource(currObj.id);
}
@Override
public int getItemCount() {
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView image;
public MyViewHolder(View itemView) {
super(itemView);
title= (TextView) itemView.findViewById(R.id.title_item);
image = (ImageView) itemView.findViewById(R.id.img_item);
}
}
}
ReViewInformation.class
public class ReViewInformation {
int id;
String Title;
}
_1Vanguard_News.class
public class _1Vanguard_News extends Fragment implements OnRefreshListener{
SwipeRefreshLayout swipeLayout;
RecyclerView recyclerView;
private ViewAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view;
view = inflater.inflate(R.layout.vanguard_news, container, false);
//view = inflater.inflate(R.layout.principal, container, false);
//Recycle list view
recyclerView = (RecyclerView) view.findViewById(R.id.feed_list);
adapter = new ViewAdapter(getActivity(),getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//Swipe to refresh view
swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
swipeLayout.setColorSchemeResources(R.color.orange, R.color.green, R.color.blue);
swipeLayout.setOnRefreshListener(this);
return view;
}
public static List<ReViewInformation> getData(){
List<ReViewInformation> data= new ArrayList();
int []icons={R.drawable.france,R.drawable.brazil,R.drawable.ic_launcher};
String []titles={"hgjhgbn","hKHk","hgdjgj"};
for(int i = 0; i <3; i++){
ReViewInformation curr = new ReViewInformation();
curr.id = icons[i];
curr.Title = titles[i];
data.add(curr);
}
return data;
}
@Override
public void onRefresh() {
// TODO Auto-generated method stu
}
}
截屏
有什麼我失蹤?