3

我需要知道,當用戶點擊一個項目(或例如whatsApp聯繫人)時,WhatsApp向用戶展示一個具有此圖片示例的設計:實現具有相同設計的RecycleView或CardView項目

這裏是循環查看項目

enter image description here

,我使用:

http://code.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android--cms-23465

Mainactivity:

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佈局:

<?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爪哇代碼的錯誤:

無法解析符號聯合,也爲人!

所以,在此之後(我希望這個固定的)。

我需要當用戶點擊每個項目,它向我們展示類似的設計,但是,與每個項目的名稱。

例子:用戶點擊項目2:Lavery Maiss

和,則跳轉至另一個活動或佈局,向我們展示了這個名字。

如果用戶點擊了項目1:Emma Wilson,它會向我們展示另一項活動,但每個項目的設計都是相同的,但帶有Emma Wilson名稱。

我該怎麼辦?我的代碼有什麼問題?

我們可以做些什麼來表現這一點?

乾杯!

回答

0

經過對此的研究,我找到了一個很好的教程,沒有人提到它。 https://github.com/tarek360/Material-Animation-Samples

所以,我們需要一個適配器這樣做的象下面這樣:

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()); 

     } 
    } 

在這些代碼看看:

// 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()); 

     } 

這裏是有這樣一個很好的例子動畫:

https://github.com/tarek360/Material-Animation-Samples/tree/master/app/src/main/java/com/tarek360/animationsamples

正是我們需要的;)

相關問題