2016-09-18 68 views
2

嗨,我嘗試創建如下頁面,如何創建水平滾動,能夠CardView

​​

我有一個抽屜佈局,並在主屏幕我想顯示通知作爲屏幕射擊。我試圖通過CardView來完成,但我沒有得到任何教程來解釋我如何在CardView中實現這一目標。我在listView中獲得了Cardview的教程,但不符合我的要求。任何人都可以告訴我如何實現這一目標?在android中有什麼所有可能的選項?

+1

我如果你想要'n'數量的卡片,使用recycleview。如果Hor.View中需要少量卡片。我會展示出來。 – W4R10CK

+0

thanku W4R ...如果你可以給我兩種方式的鏈接...我可以看到並按照簡單的一個:) – kumar

回答

1

在Android和Cardview中搜索RecyclerView。下面的例子只是一個目的,顯示如何創建像你添加的圖片。要添加nCardView的數字,請閱讀「如何在Android中使用RecyclerView」。 In this the blue rect is the area covered by Hor.View

 <HorizontalScrollView 
     android:layout_width="your_size" 
     android:layout_height="your_size" 
     android:id="@+id/horizontalScrollView" 
     android:layout_below="@+id/your_id" 
     android:scrollbars="none"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:padding="2dp"> 

      <android.support.v7.widget.CardView 
       android:layout_width="250dp" 
       android:layout_height="match_parent" 
       android:layout_marginStart="2dp" 
       android:padding="2dp" 
       android:id="@+id/cardThree" 
       android:layout_toEndOf="@+id/cardTwo" 
       android:background="@color/card_color"> 

       //your view here like a Layout including textView. 

      </android.support.v7.widget.CardView> 

      <android.support.v7.widget.CardView 
       android:layout_width="250dp" 
       android:layout_height="match_parent" 
       android:padding="2dp" 
       android:id="@+id/cardOne" 
       android:background="@color/card_color"> 

       <//your view here like a Layout including textView. 

      </android.support.v7.widget.CardView> 

      <android.support.v7.widget.CardView 
       android:layout_width="250dp" 
       android:layout_height="match_parent" 
       android:layout_marginStart="2dp" 
       android:padding="2dp" 
       android:id="@+id/cardTwo" 
       android:layout_toEndOf="@+id/cardOne" 
       android:background="@color/card_color"> 

       //your view here like a Layout including textView. 

      </android.support.v7.widget.CardView> 
     </RelativeLayout> 
    </HorizontalScrollView> 
+0

@ W4R..thanku的代碼..但這不是我在找什麼,你能用recycleview告訴我n張卡片嗎? bcz我需要動態地添加內容.. – kumar

+0

這有你需要的一切:http://www.androhub.com/android-staggered-and-horizo​​ntal-recyclerview/ – W4R10CK

0

custom_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="200dp" 
android:layout_height="120dp"> 

<android.support.v7.widget.CardView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


</android.support.v7.widget.CardView> 

<ImageView 
    android:id="@+id/imageView" 
    android:layout_width="40dp" 
    android:layout_height="40dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentTop="true" 
    android:contentDescription="@string/imageview" 
    android:src="@android:drawable/ic_menu_add" /> 

<ImageView 
    android:id="@+id/imageView2" 
    android:layout_width="40dp" 
    android:layout_height="40dp" 
    android:layout_toEndOf="@+id/imageView" 
    android:layout_toRightOf="@+id/imageView" 
    android:contentDescription="@string/imageview2" 
    android:src="@android:drawable/ic_delete" /> 

<TextView 
    android:id="@+id/textview" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/imageView2" 
    android:layout_alignStart="@+id/imageView2" 
    android:layout_below="@+id/imageView2" /> 


<TextView 
    android:id="@+id/textview2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_below="@+id/textview" 
    android:layout_margin="16dp" 
    android:gravity="center" /> 

activity_recycler_main.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:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin"> 

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

Information_Object.java

public class InformationObject { 
private String post, time; 

public String getTime() { 
    return time; 
} 

public void setTime(String time) { 
    this.time = time; 
} 

public String getPost() { 
    return post; 
} 

public void setPost(String post) { 
    this.post = post; 
} 

}

RecyclerAdapter.java

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyRecyclerHolder> { 
private LayoutInflater inflater; 
private List<InformationObject> list; 

RecyclerAdapter(Context context, List<InformationObject> list) { 
    inflater=LayoutInflater.from(context); 
    this.list = list; 
} 

@Override 
public MyRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    return new MyRecyclerHolder(inflater.inflate(R.layout.custom_layout, parent, false)); 
} 

@Override 
public void onBindViewHolder(MyRecyclerHolder holder, int position) { 
    holder.time.setText(list.get(position).getTime()); 
    holder.post.setText(list.get(position).getPost()); 
} 

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

public class MyRecyclerHolder extends RecyclerView.ViewHolder { 
    private ImageView imageview, imageview2; 
    private TextView post, time; 

    public MyRecyclerHolder(View itemView) { 
     super(itemView); 
     post = (TextView) itemView.findViewById(R.id.textview); 
     time = (TextView) itemView.findViewById(R.id.textview2); 
     imageview = (ImageView) itemView.findViewById(R.id.imageView); 
     imageview2 = (ImageView) itemView.findViewById(R.id.imageView2); 
    } 
} 

}

MainActivity.java

public class RecyclerMain extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_recycler_main); 
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv); 
    RecyclerAdapter recyclerAdapter = new RecyclerAdapter(this, getData()); 
    recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)); 
    recyclerView.setAdapter(recyclerAdapter); 
} 

private List<InformationObject> getData() { 
    //add information each of cardview 
    //load imageView by picasso 
    return null; 
} 

}