重复使用膨胀的视图,不会多次膨胀

时间:2016-08-31 14:31:39

标签: android performance android-layout listview android-inflate

我想重用一个膨胀的视图,就像listView适配器对convertView一样。我一直潜伏着适配器的源代码而没有运气。

我需要将依赖于数据的dinamically视图添加到recyclerItemView,现在我需要浏览多少次,但考虑到它内部的recyclelerView,根据数据量和滚动,此操作可能会变得非常昂贵用户的行为。

我的问题的其他解决方案是在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>

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()上崩溃了            - &gt; java.lang.IllegalStateException:指定的子级已有父级。您必须首先在孩子的父母身上调用removeView()。

2 个答案:

答案 0 :(得分:0)

您所指的问题是通过创建RecyclerView解决的问题,这是对ListView的升级。 RecyclerView所做的是它将创建/膨胀说5-10列表项,当用户开始滚动时,新的listItem视图不会膨胀,只是数据在listItem内更新。

这是一个很好的解释器视频,讲述了回收者视图如何实际使用一些代码。 https://www.youtube.com/watch?v=Wq2o4EbM74k

我希望这清楚你对通胀问题的疑虑。

答案 1 :(得分:0)

  

我的应用程序在addView() - &gt; 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 @