我正在做一个应用程序,用户可以在其中切换两种语言(英语和法语),我有一个片段,其中数据显示在列表视图中,但是由于数据因所选语言而异,我重叠了两个列表视图,并根据所选语言使其中一个可见。
现在我面临的问题是,即使两种情况下的代码几乎相同,onItemClicklistener方法也仅适用于一个列表视图。我注意到,在布局中,最后写入的listview是使该方法起作用的那个。我想知道是否有办法解决这个问题,以便两个列表视图都可以使用其方法。 这是代码,为了简化起见,我省略了某些细节:
public class ReportFragment extends Fragment {
private ListView listView;
private ListView listView_fr;
public ReportFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_report, container, false);
reportList = new ArrayList<>();
reportList_fr = new ArrayList<>();
listView = view.findViewById(R.id.listView_report);
//Listview for french reports
listView_fr = view.findViewById(R.id.listView_report_fr);
if (Locale.getDefault().getLanguage().contains("en")){
listView_fr.setVisibility(View.GONE);
listView.setVisibility(View.VISIBLE);
} else if (Locale.getDefault().getLanguage().contains("fr")){
listView_fr.setVisibility(View.VISIBLE);
listView.setVisibility(View.GONE);
}
listView.setOnItemClickListener((parent, view1, position, id) -> {
Report report = reportList.get(position);
//Opening the upload file in browser using the upload url
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(report.getUrl()));
Log.d("TAG", "link en: " + Uri.parse(report.getUrl()));
startActivity(intent);
});
listView_fr.setOnItemClickListener((parent, view12, position, id) -> {
Report_fr report = reportList_fr.get(position);
//Opening the upload file in browser using the upload url
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(report.getUrl()));
Log.d("TAG", "link fr: " + Uri.parse(report.getUrl()));
startActivity(intent);
});
return view;
}
}
和布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/unicef"
android:orientation="vertical"
tools:context=".UI.ReportFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_background"
android:gravity="center_horizontal"
android:text="@string/textview_info_report_fragment"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_marginTop="20dp"
android:id="@+id/listView_report_fr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="10.0sp"/>
<ListView
android:layout_marginTop="20dp"
android:id="@+id/listView_report"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="10.0sp"/>
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:0)
问题是您正在列表视图的根元素中使用相对布局,而不是使用此代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_marginTop="20dp"
android:id="@+id/listView_report_fr"
android:layout_width="match_parent"
android:layout_height="0dp"
android:weight="1"
android:dividerHeight="10.0sp"/>
<ListView
android:layout_marginTop="20dp"
android:id="@+id/listView_report"
android:layout_width="match_parent"
android:layout_height="0dp"
android:weight="1"
android:dividerHeight="10.0sp"/>
</LinearLayout>