【问题标题】:Unable to start fragment from onListItemClick无法从 onListItemClick 开始片段
【发布时间】:2016-03-25 20:42:36
【问题描述】:

我正在尝试从列表视图的 OnItemClickListener 事件开始一个片段,但它没有显示任何内容。为了检查 OnItemClickListener 是否正常工作,我包含了一个 Toast 来显示一些文本及其工作正常。 这是我的代码

这里是主要活动的ItemClickListener的代码

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){

            Fragment fragment = new Friendtilefragment();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.friendtilefragment, fragment);
            transaction.addToBackStack(null);
            transaction.commit();

            Toast.makeText(getApplicationContext(), "An item of the ListView is clicked.", Toast.LENGTH_LONG).show();
        }
    });

Friendtilefragment 的代码

package com.example.xxxxxxxx.socialoid;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Friendtilefragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.friendtilefragment_layout, container, false);
}
}

mainactivity_layout.xml

<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_row_selector" />

<fragment
    android:id="@+id/friendtilefragment"
    android:name="com.example.xxxxxxxx.socialoid.Friendtilefragment"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</android.support.v4.widget.SwipeRefreshLayout>

friendtilefragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="This is textview"/>

</LinearLayout>

【问题讨论】:

  • 你得到的错误是什么
  • 改变它 Friendtilefragment fragment = new Friendtilefragment();
  • 没有。没有错误,这真的很令人沮丧。
  • @NigamPatro,添加fragment时不一定要带FrameLayout。
  • @deejay,可能是你的 ListView 占据了整个屏幕 ListView 中有多少项目?

标签: android xml android-fragments android-listview


【解决方案1】:

你必须在你的 MainActivity 中创建一个FrameLayout 并将android:id="@+id/friendtilefragment" 移动到FrameLayout

mian_activity 中的示例

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/friendtilefragment">

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">

.....

在此之间,将Fragment fragment = new Friendtilefragment(); 更改为Friendtilefragment fragment = new Friendtilefragment();

查看this

【讨论】:

  • 我正在尝试这段代码.. 我需要完全删除片段标签吗?
  • 是的,你可以删除它,看看它是否有效
  • 我是指我们在聊天中讨论的问题,而不是这里
【解决方案2】:

替换

Fragment fragment = new Friendtilefragment();

有了这个

Friendtilefragment fragment = new Friendtilefragment();

【讨论】:

    【解决方案3】:

    试试这个.. 用你的mainactivity_layout.xml替换代码

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/re/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/friendtilefragment"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
    
    <android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_row_selector" />    
    </android.support.v4.widget.SwipeRefreshLayout>
    </RelativeLayout>
    

    我希望这有效..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 2017-08-17
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多