您好我尝试创建如下页面,
我有抽屉布局,在主屏幕中我想显示屏幕截图中的通知。我试图通过public class Pag1 extends Fragment {
Button buton;
TextView texto;
public Pag1(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View vista =inflater.inflate(R.layout.fragment_pag1,container,false);
buton= (Button) vista.findViewById(R.id.button);
texto= (TextView) vista.findViewById(R.id.textView2);
buton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
texto.setText("sadfasdfsad");
}
});
return vista;
}
}
来完成,但我没有得到任何教程解释如何在CardView
中实现这一点。我在CardView
中获得了Cardview
的教程,但不是我的要求。任何人都可以告诉我如何实现这一目标吗?在android中我有什么可能的选择?
答案 0 :(得分:2)
在android和Cardview中搜索RecyclerView。以下示例仅用于显示如何创建您添加的图片。添加n
个CardView
读取数量"如何在Android中使用RecyclerView"。
<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>
答案 1 :(得分: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;
}
}
答案 2 :(得分:0)
studviewseat = findViewById(R.id.seats);
studviewseat.setNestedScrollingEnabled(true);
GridLayoutManager layoutManager = new GridLayoutManager(this,10
,GridLayoutManager.HORIZONTAL, false);
studviewseat.setLayoutManager(layoutManager);
int spanCount = 10;
int spacing = 50;
boolean includeEdge = true;
studviewseat.addItemDecoration(new Gridspcae(spanCount, spacing, includeEdge));
studviewseat.setItemAnimator(new DefaultItemAnimator());
list = new ArrayList<>();
if(studviewseat.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
studviewseat.setLayoutManager(new GridLayoutManager(this, 3));
}
else if (studviewseat.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
studviewseat.setLayoutManager(new GridLayoutManager(this, 5));
}