1

リストビューアダプターがconvertViewと同じように膨張したビューを再利用したいと思います。私は運がないアダプターのソースコードに潜んでいました。2回以上膨らませずに、膨張したビューを再利用

私はビューを必要とするだけ多くの時間を膨らませていますが、リサイクルビューを内部に持っているので、この操作はデータの量に応じてばかげて高価になる可能性があり、recyclerItemViewにデータに応じてビューを追加する必要があります。ユーザーのスクロール動作。

他の解決策は、recyclerItem内でリストビューを作成することです。リストビューはスクロール可能ではなく、動的に追加されたデータに応じて最大高さに展開されます(リストビューコンテナを展開してすべてのデータを表示します) recyclerItemはその高さに応じて展開されます。

ACTIVITY

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> 

MODEL

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> 

ADAPTER

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

私の問題は、複雑なレイアウトを膨らませてaddView()を使ってlinearlayoutに追加したいからです。そうすれば、recycler-itemがlinearLayout内のこのレイアウトの4〜5倍を膨張させる必要がある場合、**これは遅くなったり、 recyclerview **をスクロールしてください。リサイクラのアイテムがポピュレートしてビューを空にし、_ウィジェットが可能なら繰り返すことを避けるためのコストのかかる操作を実行する_ため、inflate操作は何回も実行されるからです。今私はそれをリサイクル行ごとに2〜3回簡単にやります。 –

+0

ええ、ビューを膨らませるのは、ListView/RecyclerViewを作成して、毎回ビューを膨らませないようにしているのですが、データを再利用してデータをバインドするだけの理由があります。 – lelloman

答えて

0

あなたが指摘している問題は、ListViewへのアップグレードであるRecyclerViewを作成することによって解決された問題です。 RecyclerViewの機能は、5-10のリストアイテムを作成/拡張し、ユーザーが新しい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) –

関連する問題