在自定义视图中嵌入 RecyclerView

Moh*_*uag 1 java android android-custom-view android-recyclerview

我正在尝试将 a 嵌入RecyclerView自定义视图 (a RefreshableList)的正文中。

这是我的项目结构:它包含 2 个模块,apprlist

项目视图

所述RLIST模块保持(自定义视图RefreshableList),其中我要嵌入RecyclerView

RefreshableList.java(自定义视图)

public class RefreshableList extends RelativeLayout {

    private RecyclerView mRecyclerView;

    private Context mContext;
    private MyAdapter mAdapter;

    public RefreshableList(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        Log.i("ADAPTER", "RefreshableList is initializing views...");

        setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.refreshable_list, null);

        findViewsById(view);
        setupRecyclerView();
    }

    private void findViewsById(View view) {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.dataRecyclerView);
        Log.i("ADAPTER", "RecyclerView has been initialized.");
    }

    private void setupRecyclerView() {
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        Log.i("ADAPTER", "RecyclerView has been setup.");
    }

    public void setAdapter(MyAdapter adapter) {
        mAdapter = adapter;
        mRecyclerView.setAdapter(mAdapter);
        Log.i("ADAPTER", "Adapter has been set." + mAdapter.getItemCount());
    }
}
Run Code Online (Sandbox Code Playgroud)

MyAdapter.java (TheRecyclerView的适配器)

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    protected Context mContext;
    protected List<String> mItems;

    public MyAdapter(Context context, List<String> items) {
        mContext = context;
        mItems = new ArrayList<>(items);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_row, parent, false);
        Log.i("ADAPTER", "Creating row...");

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.label.setText(mItems.get(position));
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView label;

        public ViewHolder(View itemView) {
            super(itemView);
            label = (TextView) itemView.findViewById(R.id.label);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

item_row.xml: (RecyclerView元素的 XML 布局)

<?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="60dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="New Text"
        android:id="@+id/label"
        android:gravity="left|center_vertical"
        android:padding="8dp"
        android:background="#ff0000"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

refreshable_list.xml:(RefreshableList自定义视图的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="match_parent"
              android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/dataRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

所有这些代码都属于rlist模块。为了测试它,我MainActivity应用程序模块中添加了一个,我在其中嵌入了一个RefreshableList

主活动.java

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.list)
    RefreshableList mRefreshableList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        mRefreshableList.setAdapter(new MyAdapter(
                this, Arrays.asList("Java", "Android", "Python", "Kivy")));
    }
}
Run Code Online (Sandbox Code Playgroud)

活动_main.xml

<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.zouag.refreshablelist.MainActivity"
    android:background="#00ff00">

    <com.zouag.rlist.RefreshableList
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffff00"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

运行应用程序后,我看不到任何RecyclerView's 元素:(即使RecyclerView清晰可见,标有黄色)

跑

我在这里错过了什么?

And*_*sen 5

不是你的 recyclerview 是可见的,你的相对布局是可见的。您不会将膨胀的布局附加到您的视图组。

电话:

View view = inflater.inflate(R.layout.refreshable_list, null);
Run Code Online (Sandbox Code Playgroud)

只是膨胀布局,但不将其附加到视图。如果你想坚持这一点,你需要在充气后附加视图:

this.addView(view)
Run Code Online (Sandbox Code Playgroud)

或者只是打电话:

View view = inflater.inflate(R.layout.refreshable_list, this,true);
Run Code Online (Sandbox Code Playgroud)

它将膨胀的布局附加到根视图。