cardView外部的java文本视图
我想将oldTextView的功能切换到ID为NameLabel的TextView
“nameAgeLabel”位于视图之外。 它还应该像现在一样动态更改文本,具体取决于当前加载的视图
public View getView(final int position, View View, ViewGroup parent) {
Log.d("position:",String.valueOf(position));
Log.d("laenge user id",String.valueOf(userIds.size()));
String currentUserId = userIds.get(position);
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.content_single_mode_vc, parent, false);
ImageView imageView = (ImageView) view.findViewById(R.id.offer_image);
Bitmap image = userImages.get(currentUserId);
imageView.setImageBitmap(image);
TextView oldTextView= (TextView) view.findViewById(R.id.textView2);
oldTextView.setText(userNames.get(currentUserId));
//works fine
return view;
}
我的onCreate如下所示:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_mode_vc);
TextView nameAge = (TextView) findViewById(R.id.nameAgeLabel);
updateImage();
//(thats another method)
}
acvivity\u单模式\u vc。xml:
<?xml version="1.0" encoding="utf-8"?>
<com.daprlabs.aaron.swipedeck.layouts.SwipeFrameLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:swipedeck="http://schemas.安卓.com/apk/res-auto"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:id="@+id/swipeLayout"
安卓:orientation="vertical"
安卓:background="@color/white">
<安卓.support.design.widget.AppBarLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:theme="@style/AppTheme.AppBarOverlay"/>
<安卓.support.v7.widget.Toolbar
安卓:id="@+id/toolbar"
安卓:layout_width="match_parent"
安卓:layout_height="?attr/actionBarSize"
安卓:background="?attr/colorPrimary"
/>
<com.daprlabs.aaron.swipedeck.SwipeDeck
安卓:id="@+id/swipe_deck"
安卓:layout_width="match_parent"
安卓:layout_height="600dp"
安卓:padding="20dp"
安卓:layout_marginTop="60dp"
swipedeck:max_visible="3"
swipedeck:card_spacing="15dp"
swipedeck:swipe_enabled="true" >
</com.daprlabs.aaron.swipedeck.SwipeDeck>
<Button
安卓:id="@+id/close"
安卓:layout_width="100dp"
安卓:layout_height="100dp"
安卓:layout_marginBottom="50dp"
安卓:layout_marginLeft="20dp"
安卓:layout_gravity="bottom"
安卓:elevation="1dp"
安卓:background="@drawable/close"
安卓:backgroundTint="@color/light" />
<Button
安卓:id="@+id/check"
安卓:layout_width="100dp"
安卓:layout_height="100dp"
安卓:layout_marginBottom="50dp"
安卓:layout_marginRight="20dp"
安卓:layout_gravity="bottom|right"
安卓:background="@drawable/check"
安卓:backgroundTint="@color/light" />
<TextView
安卓:layout_width="match_parent"
安卓:padding="20dp"
安卓:layout_marginLeft="20dp"
安卓:layout_marginRight="20dp"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="350dp"
安卓:id="@+id/nameAgeLabel"
安卓:background="@color/separator"
/>
</com.daprlabs.aaron.swipedeck.layouts.SwipeFrameLayout>
内容\单一\模式\ vc。xml:
<?xml version="1.0" encoding="utf-8"?>
<安卓.support.v7.widget.CardView
xmlns:card_view="http://schemas.安卓.com/apk/res-auto"
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/card_view"
安卓:layout_gravity="center"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true"
安卓:layout_margin="8dp"
安卓:gravity="center_horizontal"
安卓:padding="25dp"
安卓:clipChildren="true">
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:orientation="vertical"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<RelativeLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content">
<ImageView
安卓:id="@+id/offer_image"
安卓:layout_width="match_parent"
安卓:scaleType="centerCrop"
安卓:adjustViewBounds="true"
安卓:layout_height="200dp"/>
<TextView
安卓:text="TextView"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignParentTop="true"
安卓:layout_alignParentLeft="true"
安卓:layout_alignParentStart="true"
安卓:layout_marginLeft="137dp"
安卓:layout_marginStart="137dp"
安卓:layout_marginTop="222dp"
安卓:id="@+id/textView2"/>
</RelativeLayout>
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal"
安卓:gravity="center">
<ImageView
安卓:layout_weight="1"
安卓:id="@+id/left_image"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@id/offer_image"
安卓:layout_centerHorizontal="true" />
<ImageView
安卓:layout_weight="1"
安卓:id="@+id/right_image"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/offer_image"
安卓:layout_centerHorizontal="true" />
</LinearLayout>
</LinearLayout>
</安卓.support.v7.widget.CardView>
所以,主要的问题是,我必须在哪里以及如何更改textview“nameAge”才能获得与现在相同的结果?在我的getView中,方法不起作用,因为
TextView nameAge= (TextView) view.findViewById(R.id.nameAgeLabel)
不包括ID为“nameAgeLabel”的textView。在onCreate中这样做没有多大意义。 在我的getView中创建一个新视图也不起作用,因为我无法返回2个视图,此外,我会收到一条“安卓 requestlayout unjuly..”消息
非常感谢
编辑:类名+构造函数
public class SwipeDeckAdapter extends BaseAdapter {
[...]
public SwipeDeckAdapter(LinkedList<String> data, Context context, LinkedList<String> userIds, Hashtable<String,String> userNames, Hashtable<String,String> userHashtags, Hashtable<String,Bitmap> userImages, Hashtable<String,ArrayList<String>> userAccepted) {
this.data = data;
this.context = context;
this.userIds = userIds;
this.userNames = userNames;
this.userHashtags = userHashtags;
this.userImages = userImages;
this.userAccepted = userAccepted;
}
}
# 1 楼答案
由于你的描述不完整,我试着猜测
为
SwipeFrameLayout
创建适配器,从Activity
传递TextView
。在构造函数或单独的方法中差不多