我想重複使用充氣視圖,就像listView適配器用convertView所做的一樣。我一直在潛伏適配器的源代碼,但沒有運氣。重複使用充氣視圖,不多次膨脹
我需要根據數據添加dinamically視圖到recyclerItemView,現在即時充氣盡可能多的次數,因爲我需要的視圖,但考慮到它的裏面一個recyclerView,此操作可能會變得非常昂貴取決於數據量和滾動用戶的行爲。
我的問題的其他解決方案是在recyclerItem中創建一個listview,因此listview不會滾動,根據動態添加的數據擴展到最大高度(使listview容器展開以顯示其所有數據)並使recyclerItem根據其高度展開。
活動
public class Main1Activity extends AppCompatActivity {
RecyclerView recyclerView;
CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
recyclerView = (RecyclerView) findViewById(R.id.recycler);
setUpRecycler();
}
private void setUpRecycler(){
Sample sample1 = new Sample("List of Items1");
Sample sample2 = new Sample("List of Items2");
Sample sample3 = new Sample("List of Items3");
List<Sample> sampleList = new ArrayList<>();
sampleList.add(sample1);
sampleList.add(sample2);
sampleList.add(sample3);
adapter = new CustomAdapter(this,sampleList);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:padding="30dp">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
模型
public class Sample {
String name;
public Sample(){}
public Sample(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
XML項
**item_list**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
**item_smaple**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_margin="10dp"
android:text="List of Items"
android:textSize="20sp"/>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:orientation="vertical"/>
</LinearLayout>
適配器
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private List<Sample> mySamples;
private Context mContext;
public CustomAdapter(Context context, List<Sample> mySamples) {
this.mContext = context;
this.mySamples = mySamples;
}
private Context getContext() {
return mContext;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public LinearLayout linearLayout;
public ViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
linearLayout = (LinearLayout) itemView.findViewById(R.id.linearLayout);
}
}
@Override
public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View contactView = inflater.inflate(R.layout.item_sample, parent, false);
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
@Override
public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) {
Sample sample = mySamples.get(position);
holder.name.setText(sample.getName());
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.item_list, null, false);
for (int i = 0; i < 20; i++){
TextView textView = (TextView) v.findViewById(R.id.textView);
textView.setText("hello"+i);
holder.linearLayout.addView(v);
}
}
@Override
public int getItemCount() {
return mySamples.size();
}
}
現在我正在循環內充氣。我如何修改視圖參數以使其可重用到框架?
我的應用程序崩潰了addView() - > java.lang.IllegalStateException:指定的子項已經有一個父項。您必須先調用子對象的父對象的removeView()。
有啥與使用RecyclerView問題? – lelloman
我的問題是,我想膨脹一個複雜的佈局,並將其添加到addlay()的linearlayout,因此如果回收器項目需要在linearLayout內部膨脹4-5佈局,**這可能會減慢甚至凍結滾動回收站查看**;因爲充值操作將在循環器項目填充和清空視圖時執行很多次,並且_inflate其成本高的操作,列表小部件避免在可能的情況下重複。現在,我將輕鬆地爲每個回收站排2-3次。 –
是的,膨脹意見這是一個昂貴的操作,這就是爲什麼他們使ListView/RecyclerView,以便它不會每次膨脹的意見,但它會重用他們,只是綁定數據 – lelloman