【发布时间】:2019-03-17 03:48:03
【问题描述】:
我有一个TabLayout,其中我将 3 个选项卡显示为片段。第一个选项卡加载具有ListView 的第一个片段。当我单击 ListView 的项目时,我希望将我的第一个选项卡片段替换为包含所单击项目详细信息的新片段。
这是我的实现:
我的 Activity 的 layoutout:dashboard.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ParentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".interestingReads.InterestingReadsDashboard_Activity">
<android.support.design.widget.AppBarLayout
android:id="@+id/interestingReadsAppBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/interestingReadsToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/interestingReadsTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextAppearance="@style/MyCustomSmallLettersTextAppearance"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/interestingReadsViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="165dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</FrameLayout>
fragment_rss_feeds.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
android:id="@+id/relativeLayout111"
tools:context=".interestingReads.RSSFeedsTab">
<ListView
android:id="@+id/rssFeedsListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@android:color/black"
android:dividerHeight="8dp"
android:background="@color/diffBackgroundWhite"
/>
</FrameLayout>
rss_item_list_row.xml
<?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:id="@+id/rssFeeds_LL_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
android:clickable="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="6dp"
>
<ImageView
android:id="@+id/rssFeeds_Image_list_view"
android:layout_width="60dp"
android:layout_height="60dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:contentDescription="@string/app_name"
android:padding="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="7dp">
<TextView
android:id="@+id/rssFeeds_Title_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp"
android:textStyle="bold" />
<TextView
android:id="@+id/rssFeeds_Description_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
RSSFeedsTab.java
public class RSSFeedsTab extends ListFragment implements OnItemClickListener {
.
.
.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
.
.
. @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View RootView = getActivity().getLayoutInflater().inflate(R.layout.fragment_rssfeeds_tab, container, false); //pass the correct layout name for the fragment
final ListView RSSFeedsItemLV = (ListView) RootView.findViewById(R.id.rssFeedsListView);
final LinearLayout RSSFeeds_SingleItem_LL = (LinearLayout) RootView.findViewById(R.id.rssFeeds_LL_list_view);
.
.
.
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.rss_item_list_row, from, to){
@Override
public View getView (int position, View convertView, ViewGroup parent)
{
View v = super.getView(position, convertView, parent);
final int finalposition = position;
final LinearLayout RSSFeed_singleItem=(LinearLayout) v.findViewById(R.id.rssFeeds_LL_list_view);
RSSFeed_singleItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getActivity(),"Link:==>"+localLinkArray[finalposition],Toast.LENGTH_SHORT).show();
FragmentTransaction trans = getFragmentManager().beginTransaction();
trans.replace(R.id.rssFeeds_LL_list_view, new ReadSingleRSSItem());
trans.commit();
}
});
return v;
}
};
setListAdapter(simpleAdapter);
RSSFeedsItemLV.setOnItemClickListener(this);
return super.onCreateView(inflater, container, savedInstanceState);
}
通过上述实现,我实现了这么多:
在这里,在onClick 内部,我试图用一个新片段替换我的完整(fragment_rss_feeds.xml)片段。
为此,我正在尝试:
FragmentTransaction trans = getFragmentManager().beginTransaction();
trans.replace(R.id.rssFeeds_LL_list_view, new ReadSingleRSSItem());
它仅替换 ListView 的单个项目,如快照所示。
如果我在trans.replace 中使用R.id.relativeLayout111 而不是R.id.rssFeeds_LL_list_view,它会给我错误:
未找到 id 0x7f0900b7 (id/relativeLayout111) 的视图
有人可以帮助我吗,在我的情况下我应该如何有效地使用trans.replace?
另外,我如何在trans.replace 中访问id relativeLayout111?
提前致谢!!
【问题讨论】:
标签: android listview android-fragments android-listfragment