1

我想重複使用充氣視圖,就像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()。

+0

有啥與使用RecyclerView問題? – lelloman

+0

我的問題是,我想膨脹一個複雜的佈局,並將其添加到addlay()的linearlayout,因此如果回收器項目需要在linearLayout內部膨脹4-5佈局,**這可能會減慢甚至凍結滾動回收站查看**;因爲充值操作將在循環器項目填充和清空視圖時執行很多次,並且_inflate其成本高的操作,列表小部件避免在可能的情況下重複。現在,我將輕鬆地爲每個回收站排2-3次。 –

+0

是的,膨脹意見這是一個昂貴的操作,這就是爲什麼他們使ListView/RecyclerView,以便它不會每次膨脹的意見,但它會重用他們,只是綁定數據 – lelloman

回答

0

您所指的問題是通過創建RecyclerView來解決的問題,它是對ListView的升級。 RecyclerView做的是它會創建/充氣說5-10列表項,並且當用戶開始滾動時,新的listItem視圖不會被誇大,只是數據在listItem中更新。

這是一個偉大的解釋視頻如何回收利用視圖實際工作與一點代碼以及。 https://www.youtube.com/watch?v=Wq2o4EbM74k

我希望這可以消除您對通脹問題的疑慮。

0

我的應用程序在addView() - > java.lang.IllegalStateException異常:指定的孩子已經有一個父。您必須先調用子對象的父對象的removeView()。

嘗試:

@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); 

    for (int i = 0; i < 20; i++) { 
     View v = inflater.inflate(R.layout.item_list, holder.linearLayout, false); 
     TextView textView = (TextView) v.findViewById(R.id.textView); 
     textView.setText("hello" + i); 
     holder.linearLayout.addView(v); 
    } 
} 

itemSample.xml

<?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="wrap_content" 
      android:orientation="vertical"> 

<TextView 
    android:id="@+id/name" 
    android:layout_width="wrap_content" 
    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="200dp" 
    android:layout_marginLeft="30dp" 
    android:orientation="vertical" 
    android:isScrollContainer="true" 
    /> 

</LinearLayout> 

itemList中。XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="40dp" 
    android:isScrollContainer="true" 
    android:id="@+id/txt_cont" 
    > 

<TextView 
    android:id="@+id/textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
</LinearLayout> 

Just Raw Solution

You should try answer @

+0

我想要的代碼是**只對所有視圖進行一次膨脹操作**,如果你閱讀我的問題的評論,你會看到我討論有多糟糕我在這裏發佈的代碼的性能可能是,謝謝你的時間,但這不是什麼即時通訊尋找。(事實上,我的代碼工作正如你發佈的,直到我找出一些東西了:P) –