2017-06-22 85 views
-1

我已經實施了一些回收站視圖(從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); 


} 

}

+1

郵政編碼....... –

+0

您的佈局似乎存在問題。你可以顯示你的佈局xml爲了幫助你。 –

+0

另外,從API加載後設置適配器 –

回答

-1

添加以下行來更新這些重要的一部分,如果你下載所有員工的數據後,未添加downloadEmployeeData()函數:

mAdapter.notifyDataSetChanged(); 

它必須是你是不是REF在您的downloadEmployeeData函數中添加數據並單擊搜索您的過濾器代碼刷新數據時重新調整適配器。

+0

謝謝,就是這樣:) –

+0

爲什麼downvote?我其實回答你的回答你的問題,不是嗎? – nitinkumarp

1

嘗試在你的代碼

RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.content_employee_list); 

    //LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); 
     LinearLayoutManager layoutManager = new LinearLayoutManager(mainFragment.getContext(), LinearLayoutManager.VERTICAL, false); 
     recyclerView.setLayoutManager(layoutManager); 


     mAdapter = new EmployeeAdapter(mainFragment.getContext(), employees_Array); 
     //mAdapter = new EmployeeAdapter(this, employees_Array); this was the old one 
     recyclerView.setAdapter(mAdapter); 
+0

謝謝你,還有是不是這個與我的代碼相同?只有訂單被改變。 我試過你的方法,但沒有奏效。 –

+0

@Override public long getItemId(int position){ return position; } –

相關問題