Reputation: 572
I'm trying to start a fragment from OnItemClickListener event of list view but its not showing anything. to check if the OnItemClickListener is functional or not, I've included a Toast to show some text and its working fine. Here is my code
Here is the code for ItemClickListener of main activity
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();
}
});
Code for the 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>
Upvotes: 1
Views: 79
Reputation: 694
You have to create a FrameLayout
in your MainActivity and move android:id="@+id/friendtilefragment"
to FrameLayout
.
Example in your 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">
.....
Between, change Fragment fragment = new Friendtilefragment();
to Friendtilefragment fragment = new Friendtilefragment();
Check this
Upvotes: 1
Reputation: 419
try this ..
replace the code with your 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>
I Hope this works..
Upvotes: 0
Reputation: 1806
Replace
Fragment fragment = new Friendtilefragment();
with this
Friendtilefragment fragment = new Friendtilefragment();
Upvotes: 0