我在设置TextView的可见性时遇到问题。
我有两个xmls。一个用于片段本身,另一个用于RecyclerView。 在fragment_settings.xml中,我正在设置RecyclerView。在settings_list_row.xml中,我定义了RecyclerView行。
当我创建一个新的RecyclerView行时,我有时会有一行填充TextView的两倍,有时只有一行。因此,如果它是空的,我想将一个TextView的可见性设置为消失。
因为我的View是fragment_settings.xml,所以我无法访问行视图。这有解决方案吗?非常感谢!
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.myfitindustry.myfitindustry.app.SettingsFragment">
<TextView
android:id="@+id/accountTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:textStyle="bold"
android:textColor="@color/colorPrimary"
android:text="@string/accountTitle" />
<android.support.v7.widget.RecyclerView
android:id="@+id/settings_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:focusable="true"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:id="@+id/settingTitle"
android:textColor="@color/colorBlack"
android:layout_width="match_parent"
android:textSize="16dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/settingSubtitle"
android:layout_below="@id/settingTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
onCreateView
方法@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.settings_recycler_view);
sAdapter = new SettingsAdapter(settingList);
RecyclerView.LayoutManager sLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext()) {
// Disable scrolling in the RecyclerView
@Override
public boolean canScrollVertically() {
return false;
}
};
recyclerView.setLayoutManager(sLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
prepareSettingsData();
recyclerView.setAdapter(sAdapter);
// HERE I WANT TO CHANGE THE VISIBILITY
TextView settingSubtitle = (TextView) view.findViewById(R.id.settingSubtitle);
if(!settingSubtitle.getText().equals("")) {
settingSubtitle.setVisibility(TextView.GONE);
}
return view;
}
package de.myfirstapp.app;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
/**
* Created by Johannes on 19.01.2017.
*/
public class SettingsAdapter extends RecyclerView.Adapter<SettingsAdapter.MySettingHolder> {
private List<Settings> settingList;
public class MySettingHolder extends RecyclerView.ViewHolder {
public TextView settingTitle, settingSubtitle;
public MySettingHolder(View view) {
super(view);
settingTitle = (TextView) view.findViewById(R.id.settingTitle);
settingSubtitle = (TextView) view.findViewById(R.id.settingSubtitle);
}
}
public SettingsAdapter (List<Settings> settingList) {
this.settingList = settingList;
}
@Override
public MySettingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.settings_list_row, parent, false);
return new MySettingHolder(itemView);
}
@Override
public void onBindViewHolder(MySettingHolder holder, int position) {
// Setting for one entry
Settings setting = settingList.get(position);
holder.settingTitle.setText(setting.getSettingTitle());
holder.settingSubtitle.setText(setting.getSettingSubtitle());
}
@Override
public int getItemCount() {
return settingList.size();
}
}
答案 0 :(得分:1)
为什么不在SettingsAdapter
onBindViewHolder
方法中执行此操作?想象一下,你的RecyclerView
和一堆settingSubtitle
中有大量项目,系统将如何理解你真正想要的TextView
?
@Override
public void onBindViewHolder(MySettingHolder holder, int position) {
// Setting for one entry
Settings setting = settingList.get(position);
if (setting.getSettingSubtitle().equals("")) {
holder.settingTitle.setText(setting.getSettingTitle());
holder.settingSubtitle.setVisibility(View.GONE);
} else {
holder.settingSubtitle.setVisibility(View.VISIBLE);
holder.settingTitle.setText(setting.getSettingTitle());
holder.settingSubtitle.setText(setting.getSettingSubtitle());
}
}