我想将oldTextView的功能交换为ID为nameAgeLabel的TextView。
" nameAgeLabel"在View视图之外。 它还应该像现在一样动态更改文本,具体取决于当前加载的视图。
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_single_mode_vc.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.daprlabs.aaron.swipedeck.layouts.SwipeFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:swipedeck="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/swipeLayout"
android:orientation="vertical"
android:background="@color/white">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
/>
<com.daprlabs.aaron.swipedeck.SwipeDeck
android:id="@+id/swipe_deck"
android:layout_width="match_parent"
android:layout_height="600dp"
android:padding="20dp"
android:layout_marginTop="60dp"
swipedeck:max_visible="3"
swipedeck:card_spacing="15dp"
swipedeck:swipe_enabled="true" >
</com.daprlabs.aaron.swipedeck.SwipeDeck>
<Button
android:id="@+id/close"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="20dp"
android:layout_gravity="bottom"
android:elevation="1dp"
android:background="@drawable/close"
android:backgroundTint="@color/light" />
<Button
android:id="@+id/check"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="50dp"
android:layout_marginRight="20dp"
android:layout_gravity="bottom|right"
android:background="@drawable/check"
android:backgroundTint="@color/light" />
<TextView
android:layout_width="match_parent"
android:padding="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_height="wrap_content"
android:layout_marginTop="350dp"
android:id="@+id/nameAgeLabel"
android:background="@color/separator"
/>
</com.daprlabs.aaron.swipedeck.layouts.SwipeFrameLayout>
content_single_mode_vc.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true"
android:layout_margin="8dp"
android:gravity="center_horizontal"
android:padding="25dp"
android:clipChildren="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/offer_image"
android:layout_width="match_parent"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:layout_height="200dp"/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="137dp"
android:layout_marginStart="137dp"
android:layout_marginTop="222dp"
android:id="@+id/textView2"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:layout_weight="1"
android:id="@+id/left_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/offer_image"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_weight="1"
android:id="@+id/right_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/offer_image"
android:layout_centerHorizontal="true" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
所以,主要的问题是我在哪里以及如何更改textview&#34; nameAge&#34;得到与我现在相同的结果?在我的getView方法不起作用,因为
TextView nameAge= (TextView) view.findViewById(R.id.nameAgeLabel)
不包含带有ID&#34; nameAgeLabel&#34;的textView。在onCreate中做这件事并没有多大意义。 在我的getView中创建一个新视图也不起作用,因为我不能返回2个视图,另外我得到一个&#34; android-requestlayout-improperly ..:&#34;消息。
非常感谢。
编辑:类名+构造函数
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;
}
}
答案 0 :(得分:0)
由于您的描述不完整,我尝试猜测。
为您从SwipeFrameLayout
传递TextView
的{{1}}创建适配器。在构造函数中或单独的方法。
像
这样的东西Activity