I have an ActionBarActivity which contains HorizontalScrollView to show tabs and fragment under each.
Currently the loaded fragment under each tab is PreferenceFragment, but I want it to be card (as part of a cardView and this card to contain my PreferenceFragment.
I know it is possible to put the PreferenceFragment in a container inside Activity view, but is it possible to put it in a container of Fragment ?
How to view preferenceFragment as card (cardView)
1.9k Views Asked by SagiLow At
3
There are 3 best solutions below
3
On
The layout you inflate for your fragment can be wrapped in a cardview.
Fragment
public View onCreateView(...) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.news_card_row, container, false);
return rootView;
}
xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/fssr_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</android.support.v7.widget.CardView>
0
On
The best method I found was to use nested fragments with AppCompat's getChildFragmentManager().
My code that works for the Swipe View I made with my cards and I think will work for you:
PrefsFragment.java:
public class PrefsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
}
PrefsContainerFragment.java:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PrefsContainerFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fragment prefsFragment = new PrefsFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.child_fragment, prefsFragment).commit();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_pref, container,false);
}
}
fragment_pref XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
card_view:cardCornerRadius="4dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
card_view:cardBackgroundColor="@color/cardview_light_background"
android:layout_margin="@dimen/card_padding"
tools:context=".PrefFragment"
android:clickable="false">
<FrameLayout
android:id="@+id/child_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v7.widget.CardView>
There might be an issue with the FragmentTransaction in the onCreate method, but as far as I can tell, it works because you use addPreferencesFromResource(R.xml.settings); in the onCreate of the child Fragment too. However, if it doesn't work for you, try putting it in onCreateView or onActivityCreated.
The solution will be to implement
ListViewinside the card view.Here is the card layout :
Notice the
ListViewwith the contained@android:id/listwhich is what thepreferenceFragmentrequires in order to be inflated.