Specify where ListFragment goes in activity_main.xml - android

My activity_main.xml is evenly split vertically 3 ways with LinearLayout. I would like to fill the bottom LinearLayout with a ListFragment and populate it with data.
Currently, the ListFragment ignores the 3 LinearLayout and just places itself on top of them.
LstFragment.java
public class LstFragment extends ListFragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragmentlayout, container, false);
// Create an array of string to be data source of the ListFragment
String[] datasource={"English","French","Khmer","Japanese","Russian","Chinese"};
// Create ArrayAdapter object to wrap the data source
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),R.layout.rowlayout,R.id.txtitem,datasource);
// Bind adapter to the ListFragment
setListAdapter(adapter);
// Retain the ListFragment instance across Activity re-creation
setRetainInstance(true);
return rootView;
}
// Handle Item click event
public void onListItemClick(ListView l, View view, int position, long id){
ViewGroup viewg=(ViewGroup)view;
TextView tv=(TextView)viewg.findViewById(R.id.txtitem);
Toast.makeText(getActivity(), tv.getText().toString(),Toast.LENGTH_LONG).show();
}
}
MainActivity.java
public class MainActivity extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LstFragment lstfragment=(LstFragment)getSupportFragmentManager().findFragmentByTag("lstfragment");
if(lstfragment==null){
lstfragment=new LstFragment();
FragmentTransaction transact=getSupportFragmentManager().beginTransaction();
transact.add(android.R.id.content,lstfragment,"lstfragment");
transact.commit();
}
}
}
activity_main.xml
<LinearLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="#+id/topSection">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Testing"
android:id="#+id/textView3" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="#+id/middleSection">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Yo"
android:id="#+id/textView2"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="#+id/bottomSection">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="WhatUP"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
There are two more simple .xml layout files called fragmentlayout.xml which contains a simple ListView #id/android:list and a rowlayout.xml which has a single TextView #id/textitem.

Replace
android:layout_height="fill_parent"
with this line
android:layout_height="0dp"
inside all the three child linear layouts

Reason
android.R.id.content gives you the root element of a view, without having to know its actual name/type/ID. That's why your Fragment is appearing at the top of everything.
You are trying to add your Fragment to wrong container here
transact.add(android.R.id.content,lstfragment,"lstfragment");
Solution
Add this
<FrameLayout
android:layout_width="fill_parent"
android:id="#+id/fragment_container"
android:layout_height="fill_parent">
</FrameLayout>
after 3rd LinearLayout but before closing the root LinerarLayout
then change to
transact.add(R.id.fragment_container,lstfragment,"lstfragment");
Also,
in all of the LinearLayout use this android:layout_height="wrap_content"

Related

Scene. Slide transition under views

Look at the sample. I have two scenes, which are start & end scenes.
Layout of start scene:
<?xml version="1.0" encoding="utf-8"?>
<merge
android:id="#+id/vgRoot"
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"
tools:parentTag="RelativeLayout"
tools:ignore="HardcodedText"
>
<Button
android:id="#+id/btn1"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/cardView"
android:text="Button"
android:layout_centerHorizontal="true"
/>
<Button
android:id="#+id/btnGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/btn1"
android:text="#string/go"
android:layout_centerHorizontal="true"
/>
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="16dp"
android:translationZ="3dp"
>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#3829AB"
android:padding="32dp"
>
<TextView
android:id="#+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text Title"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#android:color/white"
/>
</FrameLayout>
</android.support.v7.widget.CardView>
</merge>
There i have btn1 for which sliding transition is applied, cardView and container, which change its bounds and tvText as a body of CardView.
And the ending scene:
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="HardcodedText"
tools:parentTag="RelativeLayout"
>
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:cardCornerRadius="0dp"
>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:background="#3829AB"
android:orientation="vertical"
>
<TextView
android:id="#+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text Title"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#android:color/white"
/>
</FrameLayout>
</android.support.v7.widget.CardView>
</merge>
Fragment code with transition:
public class FragmentStartTransition extends Fragment {
#BindView(R.id.btnGo) View btnGo;
#BindView(R.id.vgRoot) ViewGroup vgRoot;
private Unbinder unbinder;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup view = (ViewGroup) inflater.inflate(R.layout.fragment_start_transition, container, false);
unbinder = bind(this, view);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
btnGo.setOnClickListener(btnGo.setOnClickListener(v -> moveNext());
}
#Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
private void moveNext() {
Scene scene = Scene.getSceneForLayout(vgRoot, R.layout.scene_end, getContext());
TransitionManager.go(scene, getTransition());
}
private Transition getTransition() {
Slide slide = new Slide(Gravity.TOP);
slide.addTarget(R.id.btn1);
ChangeBounds changeBounds = new ChangeBounds();
changeBounds.addTarget(R.id.cardView);
changeBounds.addTarget(R.id.container);
changeBounds.addTarget(R.id.tvText);
return new TransitionSet()
.setOrdering(TransitionSet.ORDERING_TOGETHER)
.addTransition(slide)
.addTransition(changeBounds)
.setDuration(1500);
}
}
fragment_start_transition:
<RelativeLayout
android:id="#+id/vgRoot"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="HardcodedText"
>
<include layout="#layout/scene_start"/>
</RelativeLayout>
Result animation:
I want to slide btn1 under the cardView and container. As you can see cardView is placed at the end of root layout to be over button. But in result animation it is slided over the cardView. Can i somehow control such z-axis relations between animated views?
tried cardView.bringToFront() - it doesn't help
there definetely should be simple solution, but i can't find it.
I need your help, guys.
Update:
This is because framework draws slide transition on the scene container view overlay, look at android.transition.Visibility class:
sceneRoot.getOverlay().add(overlayView);
But the question is still opened.
I just solved the same issue. The trick seems to be to not let the Visibility class copy the sliding (disappearing) view to the scene root's overlay, which will obviously be displayed above any other view in the scene.
The sliding view is only copied to the overlay if it is actually removed from the view hierarchy. If I instead leave the view in the scene but set its visibility to GONE, it re-uses the same view instead of copying it to the overlay. Then it will slide beneath its sibling views if it is below them (so your cardView.bringToFront() should work).

View Pager is empty only at first time

I have a fragment that contains recycle view and on every item press another fragment is shown that holding a view pager that shows fragment on every swipe. when I first enter to the fragment after pressing the recycle view item the pager is not visible.
on second time and the rest of the times the data is shown as expected.
code:
main fragment holds view pager:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lineItems"
style="#style/TitleTextView"/>
<android.support.v4.view.ViewPager
android:id="#+id/CardTablepager"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"/>
</LinearLayout>
content fragment (view pager swipes this fragment) :
<LinearLayout 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/poMainlayout">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/CardViewStyle"
android:layout_margin="8dp"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/poLineItemsTable"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarStyle="outsideInset"
/>
</android.support.v7.widget.CardView>
</LinearLayout>
I read few posts with no help: Android Viewpager display wrong data on first load
I also tried to invalidate the adapter from the main fragment with no help:
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewPager.getAdapter().notifyDataSetChanged();
}
any ideas?
Just notify your adapter onCreateView not onViewCreated
#Override
public void onCreateView(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewPager.getAdapter().notifyDataSetChanged();
}
Try below changes to your viewpager,
<android.support.v4.view.ViewPager
android:id="#+id/CardTablepager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="600dp"
android:layout_weight="1"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
And for RecyclerView set following properties:
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);

highlight an imageview on gridview when long press select

I have a gridview that shows a list of images, and I implemented choice mode listener for this gridview. The problem is, when I press long click to select set of images, they are not highlighted..
item_photo.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="#+id/photo"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:contentDescription="#string/media_thumbnail"/>
fragment_photos.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Empty View -->
<RelativeLayout
android:id="#+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<ImageView
android:id="#+id/download_arrow"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerInParent="true"
android:contentDescription="#string/downloads"
android:src="#drawable/ic_photo"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/download_arrow"
android:layout_centerInParent="true"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:gravity="center"
android:text="#string/no_media"/>
</RelativeLayout>
<!-- Photos List -->
<GridView
android:id="#+id/list"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:paddingBottom="5dp"
android:choiceMode="multipleChoiceModal"
android:paddingTop="5dp"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
android:visibility="gone"/>
</RelativeLayout>
PhotosFragment.java
public class PhotosFragment extends Fragment
{
#Bind (R.id.list)
GridView mGridView;
private MediaGridAdapter mAdapter;
private ArrayList<File> mFiles;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_media, container, false);
ButterKnife.bind(this, view);
mGridView.setMultiChoiceModeListener(new MultiChoiceModeListener());
// set adapter and fetch files code ....
return view;
}
}
There is no magic highlight effect, you have to implement it yourself, example would be setting a foreground drawable which is a semi transparent gray.

FAB position changes when listview's setAdapter method is called

I'm using this library for the fab implementation.
As you can see in this video, the fab position is changing when the fragment loads.
After debugging it, I found that the listview "setAdapter()" method causes the position change.
I don't have a clue why it's happening..
My layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/AppTheme.ActionBar" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#id/toolbar">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sad_face"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_leagues" />
</LinearLayout>
</FrameLayout>
<com.melnykov.fab.FloatingActionButton
android:id="#+id/fab_new_league"
android:src="#drawable/ic_action_add"
app:fab_colorNormal="#color/orange"
app:fab_colorPressed="#color/redDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#id/toolbar"
android:layout_marginBottom="-28dp"
android:layout_marginRight="28dp"
android:elevation="8dp"/>
</RelativeLayout>
My fragment
public class LeagueListFragment extends MyFragment {
#InjectView(android.R.id.list) ListView mListView;
#InjectView(android.R.id.empty) LinearLayout mEmpty;
public static Fragment newInstance() {
return new LeagueListFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_league_list, container, false);
ButterKnife.inject(this, view);
// Fetch league list and set up adapter
List<League> list = League.getAll();
mAdapter = new LeagueAdapter(getActivity(), list);
// Set up ListView
mListView.setEmptyView(mEmpty);
mListView.setDivider(new ColorDrawable(getResources().getColor(R.color.gray)));
mListView.setDividerHeight(2);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(this);
return view;
}
}
Margin changes in the runtime on pre-Lollipop because shadow mustn't be taken into account when margins are set.
Try to disable the shadow by :
fab:fab_shadow="false"
Fixing the issue is not expected for now.. (See makovkastar answer)

Fragment containing customized ListView

I am stuck, I dont get how to implement a ListView that is inside a Fragment, I am trying to make a chat gui.
Is it possible to do something like this or am I totaly out in the wind?
Thanks for help! Regards a newbie! ^^
Here is the Fragment code:
public static class ChatFragment extends Fragment {
public ChatFragment() {
// Empty constructor required for fragment subclasses
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_chat, container, false);
/*FragmentManager fm = getActivity().getFragmentManager();
DataListFragment list = new DataListFragment();
fm.beginTransaction().add(R.id.list_message_History, list).commit();
*/
getActivity().setTitle(R.string.chat);
return rootView;
}
}
But as u see I dont udnerstand how to connect it all. I have med a customized ListFragment that uses a ArrayAdapter to handle the data.
Here is my layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#id/android:list"
android:orientation="horizontal">
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"/>
</LinearLayout>
<LinearLayout 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="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
</LinearLayout>
</LinearLayout>
You don't have to add another Fragment inside your ChatFragment to just have a ListView. Get a reference for the ListView out of your layout like this:
ListView list = (ListView)rootView.findViewById(R.id.list);
Now set the adapter and the Array/List to it and you're done.
And don't make a Fragment transaction inside a Fragment. Use your Activity and the DrawerLayout for this job.
No need of ChatFragment
Make you activity a FragmentActivity and directly inflate the layout(R.layout.fragment_chat) with the ListFragment in that Activity.
class MainActivity extends FragmentActivity {
onCreate(Bundle extra) {
...
setContentView(R.layout.fragment_chat);
...
}
}
Check this example for using ListFragments.

Categories

Resources