drakeet / MultiType

Flexible multiple types for Android RecyclerView.
Apache License 2.0
5.76k stars 751 forks source link

添加Header Header是一个包含RecyclerView的布局,发现Header的RecyclerView Item显示有问题 #217

Closed Greathfs closed 6 years ago

Greathfs commented 6 years ago

What kind of issue is this?

Info:
Description:

我们这里是这样的一个布局 image

我想的整体是一个RecyclerView 上面那块是Header (包含Banner+RecyclerView) 添加Header的方式是Items添加的第一个是Header这种方式 结果上面右侧RecyclerView显示有点问题,我用demo的例子试了试,也是一样的 以下代码我是在demo的MultiSelectActivity基础上测试的(Header只有一个RecyclerView)

代码

MultiSelectActivity类

public class MultiSelectActivity extends MenuBaseActivity {

    private static final int SPAN_COUNT = 5;
    Items items = new Items();
    MultiTypeAdapter adapter;
    Button fab;
    private TreeSet<Integer> selectedSet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_select);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
        final GridLayoutManager layoutManager = new GridLayoutManager(this, SPAN_COUNT);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return (items.get(position) instanceof Category) ? SPAN_COUNT : 1;
            }
        });

        selectedSet = new TreeSet<>();

        recyclerView.setLayoutManager(layoutManager);
        adapter = new MultiTypeAdapter();
        adapter.register(Head.class, new HeadViewBinder(this));
        adapter.register(Category.class, new CategoryItemViewBinder());
        adapter.register(Square.class, new SquareViewBinder(selectedSet));

        loadData();

        assertAllRegistered(adapter, items);
        recyclerView.setAdapter(adapter);

        setupFAB();
    }

    private void loadData() {

        Head head = new Head();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add("test" + i);
        }

        head.setTitle(list);
        items.add(head);

        Category spacialCategory = new Category("特别篇");
        items.add(spacialCategory);
        for (int i = 0; i < 7; i++) {
            items.add(new Square(i + 1));
        }
        Category currentCategory = new Category("本篇");
        items.add(currentCategory);
        for (int i = 0; i < 100; i++) {
            items.add(new Square(i + 1));
        }
        adapter.setItems(items);
        adapter.notifyDataSetChanged();
    }

    private void setupFAB() {
        fab = (Button) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                StringBuilder content = new StringBuilder();
                for (Integer number : selectedSet) {
                    content.append(number + ' ');
                }
                Toast.makeText(v.getContext(),
                    "Selected items: " + content, Toast.LENGTH_SHORT)
                    .show();
            }
        });
    }
}

Head 类

public class Head {

    private List<String> title;

    public List<String> getTitle() {
        return title;
    }

    public void setTitle(List<String> title) {
        this.title = title;
    }

HeadViewBinder类

public class HeadViewBinder extends ItemViewBinder<Head, HeadViewBinder.ViewHolder> {

    private HeadAdapter mHeadAdapter;
    private Context mContext;

    public HeadViewBinder(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_head, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull Head head) {
        mHeadAdapter = new HeadAdapter(head.getTitle());
        holder.mRecyclerView.setLayoutManager(new GridLayoutManager(mContext,5));
        holder.mRecyclerView.setAdapter(mHeadAdapter);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private RecyclerView mRecyclerView;

        ViewHolder(View itemView) {
            super(itemView);
            mRecyclerView = (RecyclerView) itemView.findViewById(R.id.rv_item);
        }
    }
}

HeadAdapter类

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

    private List<String> mStrings;

    public HeadAdapter(List<String> list) {
        mStrings = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_head_layout, parent, false);
        return new ViewHolder(view);
    }

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

    @Override
    public int getItemCount() {
        return mStrings == null ? 0 : mStrings.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView mTextView;

        public ViewHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.tv_item_title);
        }
    }
}

item_head.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="520dp">

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

</LinearLayout>

item_head_layout.xml类

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="300dp"
              android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv_item_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="45454"
        android:paddingTop="20dp"
        android:paddingBottom="20dp"
        android:gravity="center_horizontal"
        android:textSize="30sp"/>

</LinearLayout>
Greathfs commented 6 years ago

问题截图显示.zip

Greathfs commented 6 years ago

测试了几次,发现是上面那个RecyclerView的宽度不正确导致的显示不正确 image 我主动给他设置宽度就能实现全屏显示了

但是为什么显示的宽度不正确目前还没找到原因