我已經實施了一些回收站視圖(從API下載)。當我運行應用程序或去這個片段我沒有看到任何項目,除非我在搜索查看開始打字(搜索查看和其他東西的功能是確定)Recyclerview不會顯示,除非我在搜索視圖中輸入
請找到這裏的Gif enter image description here
我我正在使用Volley下載數據和Picasso下載圖像。
我可能在哪裏出錯?
注:這事發生了幾次,它顯示的項目自然也沒有問題(但在100次就像3次)
這裏是我的fragment.xml之佈局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abed.whitelabel.Fragments.MainFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/search_view">
</android.support.v7.widget.SearchView>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/content_employee_list"></android.support.v7.widget.RecyclerView>
</LinearLayout>
</FrameLayout>
主要我MainFragment.java類的部分:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
downloadEmployeeData();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
MainFragment.setMainFragment(this);
sv = (SearchView)view.findViewById(R.id.search_view);
RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.content_employee_list);
mAdapter = new EmployeeAdapter(mainFragment.getContext(), employees_Array);
//mAdapter = new EmployeeAdapter(this, employees_Array); this was the old one
recyclerView.setAdapter(mAdapter);
//LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
LinearLayoutManager layoutManager = new LinearLayoutManager(mainFragment.getContext(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
//Events for searching through list
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
mAdapter.getFilter().filter(newText);
return false;
}
});
return view;
}
public void downloadEmployeeData() {
// here I download the data and fill the array
}
public class EmployeeAdapter extends RecyclerView.Adapter<EmployeesDataViewHolder> implements Filterable {
Context c;
ArrayList<EmployeesInfo> mEmployeesInfo;
ArrayList<EmployeesInfo> currentList;
public EmployeeAdapter(Context c, ArrayList<EmployeesInfo> mEmployeesInfo){
this.c = c;
this.mEmployeesInfo = mEmployeesInfo;
this.currentList = mEmployeesInfo;
}
@Override
public void onBindViewHolder(EmployeesDataViewHolder holder, int position) {
final EmployeesInfo employee = this.mEmployeesInfo.get(position);
holder.updateUI(employee);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity mainActivity = (MainActivity)getActivity();
mainActivity.loadDetailsFragment(employee);
}
});
}
@Override
public int getItemCount() {
return mEmployeesInfo.size();
}
@Override
public EmployeesDataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View card = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_employees, parent, false);
//VolleyGet.getInstance(getA)
return new EmployeesDataViewHolder(card);
}
// FOR search stuff
@Override
public Filter getFilter() {
return FilterHelper.newInstance(currentList,this);
}
public void setEmployeesInfo(ArrayList<EmployeesInfo> filteredEmployees)
{
this.mEmployeesInfo = filteredEmployees;
}
}
而且我DataViewHolder:
公共類EmployeesDataViewHolder擴展RecyclerView.ViewHolder {
private ImageView mainPhoto;
private TextView firstName;
private TextView lastName;
public EmployeesDataViewHolder(View itemView){
super(itemView);
mainPhoto = (ImageView)itemView.findViewById(R.id.list_photo);
firstName = (TextView)itemView.findViewById(R.id.list_firstName);
lastName = (TextView)itemView.findViewById(R.id.list_lastName);
}
public void updateUI(EmployeesInfo employee){
firstName.setText(employee.getFirstName());
lastName.setText(employee.getLastName());
Picasso.with(mainPhoto.getContext()).load(employee.getPhoto()).into(mainPhoto);
}
}
郵政編碼....... –
您的佈局似乎存在問題。你可以顯示你的佈局xml爲了幫助你。 –
另外,從API加載後設置適配器 –