实现一个RecycleView或CardView项目具有相同设计的海誓山盟 [英] Implementing an RecycleView or CardView items with the same design for eachother

查看:378
本文介绍了实现一个RecycleView或CardView项目具有相同设计的海誓山盟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道,当一个项目的用户点击(或如WhatsApp的触点)的WhatsApp向用户显示一个设计与此图片的例子:

I need to know, when the user clicking on one item(or for example, whatsApp contacts) WhatsApp showing to the user one design with this picture example:

这里是回收视图中的项目:

here is the recycle view items:

和我使用:

<一个href=\"http://$c$c.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android--cms-23465\" rel=\"nofollow\">http://$c$c.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android--cms-23465

主要活动:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView rv = (RecyclerView)findViewById(R.id.rv);
        rv.setHasFixedSize(true);
        LinearLayoutManager llm = new LinearLayoutManager(context);
        rv.setLayoutManager(llm);
        RVAdapter adapter = new RVAdapter(persons);
        rv.setAdapter(adapter);
    }
}

Mainactivity XML布局:

Mainactivity Xml layout:

<?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:padding="16dp"
    android:orientation="vertical">
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cv"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            >
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/person_photo"
                android:src="@mipmap/ic_launcher"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="16dp"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/person_name"
                android:layout_toRightOf="@+id/person_photo"
                android:layout_alignParentTop="true"
                android:textSize="30sp"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/person_age"
                android:layout_toRightOf="@+id/person_photo"
                android:layout_below="@+id/person_name"
                />
        </RelativeLayout>
    </android.support.v7.widget.CardView>
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rv"/>
</LinearLayout>

这里是适配器:

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder>{

        @Override
        public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_main, viewGroup, false);
            PersonViewHolder pvh = new PersonViewHolder(v);
            return pvh;
        }

        @Override
        public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
            personViewHolder.personName.setText(persons.get(i).name);
            personViewHolder.personAge.setText(persons.get(i).age);
            personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
        }
        @Override
        public void onAttachedToRecyclerView(RecyclerView recyclerView) {
            super.onAttachedToRecyclerView(recyclerView);
        }

        @Override
        public int getItemCount() {
            return persons.size();
        }
        List<Person> persons;

        RVAdapter(List<Person> persons){
            this.persons = persons;
        }

        public static class PersonViewHolder extends RecyclerView.ViewHolder {
            CardView cv;
            TextView personName;
            TextView personAge;
            ImageView personPhoto;

            PersonViewHolder(View itemView) {
                super(itemView);
                cv = (CardView)itemView.findViewById(R.id.cv);
                personName = (TextView)itemView.findViewById(R.id.person_name);
                personAge = (TextView)itemView.findViewById(R.id.person_age);
                personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
            }
        }

    }

目前,没有在 MainActivity 的Java codeS的错误:

currently, there is an error in MainActivity Java codes:

无法解析符号客户服务联系,也为个人!

cannot resolve symbol Contect and also for Person!

所以,在此之后(我希望这个固定的)。

So, after this (i hope this fixed).

我要当用户点击的每一个项目,它展示给我们类似的设计,但是,在每件商品的名称。

I need to when user clicked on the each item, It showing to us similar design, But, With names for each item.

示例:用户点击了第2项:拉维Maiss

Example: user clicked on item 2: Lavery Maiss

和则转另一个活动或布局,向我们展示了这个名字。

and then goto another activity or layout and show us this name.

如果用户点击了第1项:艾玛·威尔逊它告诉我们在另一活动相同的设计为每个项目,但是,与艾玛·威尔逊的名字

and if user clicked on the item 1: Emma Wilson it show us in another activity same design for each item, But, with Emma Wilson name.

我应该怎么做,有什么错我的codeS?

What should i do and what's wrong with my codes?

我们能为这个展示做?

干杯!

推荐答案

研究这个之后,我发现了一个很好的教程,这是没有人提到它。
https://github.com/tarek360/Material-Animation-Samples

After researching about this, i found a good tutorial which is no one mention it. https://github.com/tarek360/Material-Animation-Samples

所以,我们需要一个适配器这样做的象下面这样:

So, We need a Adaptor for doing this like below:

public class BlogRecyclerAdapter extends 

    RecyclerView.Adapter<BlogRecyclerAdapter.SimpleItemViewHolder> {

        private List<Blog> items;

        // Provide a reference to the views for each data item
        // Provide access to all the views for a data item in a view holder
        public final static class SimpleItemViewHolder extends RecyclerView.ViewHolder {
            ImageView image;
            TextView title;
            TextView subTitle;
            CardView cardView;

            public SimpleItemViewHolder(View itemView) {
                super(itemView);
                image = (ImageView) itemView.findViewById(R.id.imageThumb);
                title = (TextView) itemView.findViewById(R.id.title);
                subTitle = (TextView) itemView.findViewById(R.id.subTitle);
                cardView = (CardView) itemView.findViewById(R.id.cardView);
            }
        }

        // Provide a suitable constructor (depends on the kind of dataset)
        public BlogRecyclerAdapter(List<Blog> items) {
            this.items = items;
        }

        // Return the size of your dataset (invoked by the layout manager)
        @Override
        public int getItemCount() {
            return this.items.size();
        }

        // Create new items (invoked by the layout manager)
        // Usually involves inflating a layout from XML and returning the holder
        @Override
        public SimpleItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View itemView = LayoutInflater.from(viewGroup.getContext()).
                    inflate(R.layout.blog_item, viewGroup, false);
            return new SimpleItemViewHolder(itemView);
        }

        // Replace the contents of a view (invoked by the layout manager)
        // Involves populating data into the item through holder
        @Override
        public void onBindViewHolder(SimpleItemViewHolder viewHolder, int position) {

            viewHolder.image.setImageResource(items.get(position).getImageRes());
            viewHolder.image.setTag(position);
            viewHolder.title.setText(items.get(position).getTitle());
            viewHolder.subTitle.setText(items.get(position).getSubTitle());
            viewHolder.cardView.setCardBackgroundColor(items.get(position).getBackGroundColor());

        }
    }

看看这些codeS:

// Replace the contents of a view (invoked by the layout manager)
        // Involves populating data into the item through holder
        @Override
        public void onBindViewHolder(SimpleItemViewHolder viewHolder, int position) {

            viewHolder.image.setImageResource(items.get(position).getImageRes());
            viewHolder.image.setTag(position);
            viewHolder.title.setText(items.get(position).getTitle());
            viewHolder.subTitle.setText(items.get(position).getSubTitle());
            viewHolder.cardView.setCardBackgroundColor(items.get(position).getBackGroundColor());

        }

下面是用动画做一个很好的例子:

Here is a good example for doing this with Animation:

<一个href=\"https://github.com/tarek360/Material-Animation-Samples/tree/master/app/src/main/java/com/tarek360/animationsamples\" rel=\"nofollow\">https://github.com/tarek360/Material-Animation-Samples/tree/master/app/src/main/java/com/tarek360/animationsamples

正是我们需要的;)

这篇关于实现一个RecycleView或CardView项目具有相同设计的海誓山盟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆