【问题标题】:Android: Click ListItem of ListView to replace a FragmentAndroid:点击ListView的ListItem替换一个Fragment
【发布时间】: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


    【解决方案1】:

    您需要在父布局中添加一个容器,在这种情况下,它是包含您的选项卡的活动。比如下面提到的一个Framelayout

           <FrameLayout
            android:id="@+id/detail"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    

    那么您需要使用

    将您的 RSS 详细信息片段放入此容器中
            getFragmentManager().beginTransaction();
                            trans.replace(R.id.detail, new ReadSingleRSSItem()).commit();
    

    【讨论】:

    • 嗨@Fahad,我使用FrameLayout 而不是CoordinatorLayout,并按照建议使用trans.replace(R.id.detail, new ReadSingleRSSItem())。它不会删除我的完整 ListView 以及显示 ReadSingleRSSItem
    • 嗨@Fahad:你能建议我如何在trans.replace 中访问id relativeLayout111 吗?
    • 请确保您的详细 FrameLayout 是父容器的子容器,其中还包括详细 Framelayout 和 Tabs 布局。 @OmSao
    【解决方案2】:

    使用R.id.relativeLayout111 的问题在于,它位于 Fragment 的内部,而该 Fragment 位于 某个容器中的活动中。 解决方案是改为使用该容器的 id。

    您尚未发布包含活动的代码,因此我目前无法知道该 ID 是什么。希望你明白我在说什么。基本上只是替换:

    getFragmentManager().beginTransaction();
                            trans.replace(R.id.rssFeeds_LL_list_view, new ReadSingleRSSItem());
    

    有正确的ID!

    【讨论】:

    • 嗨@Vucko,感谢您的解释。我添加了负责包含 3 个选项卡的整页的 Activity 布局。 FrameLayout 的 id 是 ParentContainer 当我尝试 getFragmentManager().beginTransaction(); trans.replace(R.id.ParentContainer, new ReadSingleRSSItem()); 它什么也没做 :( 我还缺少什么吗?
    • 嗨@Vucko,你能建议我如何在trans.replace 中访问id relativeLayout111 吗?
    • 你应该尝试使用其他容器而不是最外面的FrameLayout。创建另一个空布局并在活动中尝试。使其低于或高于ViewPager
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    相关资源
    最近更新 更多