android - 无法从 onListItemClick 开始 fragment

标签 android xml android-fragments android-listview

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

这是主要 Activity 的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();
        }
    });

Friendtile fragment 的代码

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>

最佳答案

您必须在 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

关于android - 无法从 onListItemClick 开始 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34367480/

相关文章:

xml - 如何使用 Java API 在 Marklogic 中搜索 XML 标签或 JSON 键

android - 如何修复 FragmentManager 已经在 FragmentTransaction 中执行事务?

java - 如何在 android 中使用后台服务触发 wifi 状态更改?

java - 读取 XML 模板时 StringTemplate 无效字符 '<'

android - ViewGroup{TextView,...}.getMeasuredHeight 给出的错误值小于实际高度

java - 第一个应用程序(developer.android.com)错误,什么问题?

android-fragments - 使用 ActionBar 选项卡在 ViewPager 中的片段更改时重置 SearchView

android - FragmentPagerAdapter - 当用户转到新标签时如何检测滑动或标签点击?

java - 将 Parse.com 中的日期设置为与系统日期相同的类型

android - SQLite 创建具有唯一列组合的表