This is my screenshot 我是Android开发的初学者,现在我正在开发一个应用程序,我PagerAdapter
使用card view
,实际上我引用此链接https://rubensousa.github.io/2016/08/viewpagercards ,我没有办法处理按钮。请允许任何人帮助我这样做,因为我是初学者和学习者。
public class CardPagerAdapter extends PagerAdapter implements CardAdapter {
private List<CardView> mViews;
private List<CardItem> mData;
private float mBaseElevation;
Context ctx;
public CardPagerAdapter() {
mData = new ArrayList<>();
mViews = new ArrayList<>();
}
public void addCardItem(CardItem item) {
mViews.add(null);
mData.add(item);
}
public float getBaseElevation() {
return mBaseElevation;
}
@Override
public CardView getCardViewAt(int position) {
System.out.println("getPosition" + position);
return mViews.get(position);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = LayoutInflater.from(container.getContext())
.inflate(R.layout.adapter, container, false);
container.addView(view);
bind(mData.get(position), view);
CardView cardView = (CardView) view.findViewById(R.id.cardView);
if (mBaseElevation == 0) {
mBaseElevation = cardView.getCardElevation();
}
cardView.setMaxCardElevation(mBaseElevation * MAX_ELEVATION_FACTOR);
mViews.set(position, cardView);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
mViews.set(position, null);
}
private void bind(final CardItem item, View view) {
TextView titleTextView = (TextView) view.findViewById(R.id.titleTextView);
titleTextView.setText(item.getTitle());
}
}
这是代码
答案 0 :(得分:0)
您所说的按钮是否在卡片视图中?如果你的意思是,那么。
从我在您所指的链接中看到的内容,然后我查看了示例。 https://github.com/rubensousa/ViewPagerCards/
该按钮位于adapter.xml中,但它仍然没有id。您可以为该按钮指定一个ID:
android:id="@+id/card_button"
然后,您可以通过在java类中设置单击侦听器来处理该按钮。例如:
Button theButton = (Button) view.findViewById(R.id.card_button);
theButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//The Action Here
}
});
希望这清楚你的问题。
答案 1 :(得分:0)
Adapter.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:cardUseCompatPadding="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="24dp">
<TextView
android:id="@+id/titleTextView"
style="@style/TextAppearance.AppCompat.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/contentTextView"
style="@style/TextAppearance.AppCompat.Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"/>
<Button
android:id="@+id/btn_click"
style="@style/ButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Button" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|end"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="-5dp"
android:src="@drawable/ic_bookmark_24dp" />
</FrameLayout>
</android.support.v7.widget.CardView>
在CardPagerAdapter.java
public class CardPagerAdapter extends PagerAdapter implements CardAdapter {
Button btn_click;
private List<CardView> mViews;
private List<CardItem> mData;
private float mBaseElevation;
Context mContext;
public CardPagerAdapter(MainActivity mainActivity) {
mData = new ArrayList<>();
mViews = new ArrayList<>();
mContext = mainActivity;
}
public void addCardItem(CardItem item) {
mViews.add(null);
mData.add(item);
}
public float getBaseElevation() {
return mBaseElevation;
}
@Override
public CardView getCardViewAt(int position) {
return mViews.get(position);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
View view = LayoutInflater.from(container.getContext())
.inflate(R.layout.adapter, container, false);
container.addView(view);
bind(mData.get(position), view);
CardView cardView = (CardView) view.findViewById(R.id.cardView);
if (mBaseElevation == 0) {
mBaseElevation = cardView.getCardElevation();
}
cardView.setMaxCardElevation(mBaseElevation * MAX_ELEVATION_FACTOR);
mViews.set(position, cardView);
btn_click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "List Position " + position, Toast.LENGTH_SHORT).show();
}
});
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
mViews.set(position, null);
}
private void bind(CardItem item, View view) {
TextView titleTextView = (TextView) view.findViewById(R.id.titleTextView);
TextView contentTextView = (TextView) view.findViewById(R.id.contentTextView);
btn_click = (Button) view.findViewById(R.id.btn_click);
titleTextView.setText(item.getTitle());
contentTextView.setText(item.getText());
}
}
您必须在适配器中更改添加上下文。
MainActivity.java
mCardAdapter = new CardPagerAdapter(MainActivity.this);
这将有所帮助