【问题标题】:TextView outside of a cardViewCardView 之外的 TextView
【发布时间】:2017-08-23 13:18:43
【问题描述】:

我想将 oldTextView 的功能交换到 ID 为 nameAgeLabel 的 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_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>

所以,主要问题是我必须在哪里以及如何更改文本视图“nameAge”才能获得与现在相同的结果?在我的 getView 方法中不起作用,因为

TextView nameAge= (TextView) view.findViewById(R.id.nameAgeLabel)

不包括 ID 为“nameAgeLabel”的 textView。在 onCreate 中这样做没有多大意义。 在我的 getView 中创建新视图也不起作用,因为我无法返回 2 个视图,另外我收到“android-requestlayout-improperly..:”消息。

非常感谢。

编辑:类名+构造函数

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;
}
}

【问题讨论】:

    标签: java android layout textview


    【解决方案1】:

    由于你的描述不完整,我试着猜测一下。

    SwipeFrameLayout 创建一个适配器,您可以在其中从Activity 传递TextView。在构造函数或单独的方法中。

    类似

     public class CustomSwipeDeckAdapter extends SwipeDeckAdapter {
    
        private AppCompatTextView mNameAgeLabel;
    
        public SwipeDeckAdapter(List<String> data, Context context, AppCompatTextView textview) {
          mNameAgeLabel = textView;
          […]
        }
    
        public View getView(final int position, View View, ViewGroup parent) {
          […]
          mNameAgeLabel.setText();
        }
    

    【讨论】:

    • 感谢您的快速回复。是的,我忘了添加我的 SwipeDeckAdapter 构造函数。我在第一篇文章中对其进行了编辑。你还有什么想念的吗?我用构造函数尝试了它,但是在修复了 nullpointerexception 之后,因为 mNameAgeLabel 为空,用户名仍然没有显示在我的 mNameAgeLabel 中。它是否可能不是 AppCompatTextView 中的正确上下文?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    相关资源
    最近更新 更多