Ilya Ilya - 4 months ago 22
Android Question

ListView item open a new fragment

I am making a fitness app, where I want to have 3 nesting :


  1. ListView
    with muscle group items

  2. ListView
    of exercises (appears when to click on item from the muscle group
    ListView
    )

  3. and description of exercise.



Something like this design:

But I don't know how do realize this.
Do I need create a new fragment to each item or I can use
ViewPager
here(if yes,how to do this)?
Give me advice,please, how to realize this design (any links of the same structure projects or any other examples)
thank you in advance

Answer

Here is the sample code package com.sw.gitans201608042027;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class MainActivity extends FragmentActivity {
    private static MainActivity mCurrent = null;

    public static MainActivity getInstance() {
        return mCurrent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCurrent = this;
        setContentView(R.layout.activity_main);
        switchFragment(new ListFragment());
    }

    public void switchFragment(Fragment f) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.container, f);
        transaction.commit();
    }
}

package com.sw.gitans201608042027;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ListFragment extends Fragment implements OnItemClickListener {

    private String[] contents = { "a", "b" };
    private String[] aArr = { "a1", "a2" }, bArr = { "b1", "b2" };
    private SampleAdapter mAdapter;

    public ListFragment(String[] contents) {
        this.contents = contents;
    }

    public ListFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.lv, container, false);
        mAdapter = new SampleAdapter(getActivity(), contents);
        ((ListView) v.findViewById(R.id.lv)).setAdapter(mAdapter);
        ((ListView) v.findViewById(R.id.lv)).setOnItemClickListener(this);
        return v;
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        if (mAdapter.getItem(arg2) != null) {
            if (((String) mAdapter.getItem(arg2)).equals("a") || ((String) mAdapter.getItem(arg2)).equals("b")) {
                if (((String) mAdapter.getItem(arg2)).equals("a")) {
                    if (MainActivity.getInstance() != null)
                        MainActivity.getInstance().switchFragment(new ListFragment(aArr));
                } else {
                    if (MainActivity.getInstance() != null)
                        MainActivity.getInstance().switchFragment(new ListFragment(bArr));
                }
            } else {
                if (MainActivity.getInstance() != null)
                    MainActivity.getInstance().switchFragment(new TextFragment((String) mAdapter.getItem(arg2)));
            }
        }
    }

}

package com.sw.gitans201608042027;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class SampleAdapter extends BaseAdapter {

    private String[] contents;
    private LayoutInflater mInflater;

    public SampleAdapter(Context ctxt, String[] contents) {
        this.contents = contents;
        mInflater = LayoutInflater.from(ctxt);
    }

    @Override
    public int getCount() {
        if (contents != null)
            return contents.length;
        else
            return 0;
    }

    @Override
    public Object getItem(int position) {
        if (contents == null || position >= contents.length)
            return null;
        else
            return contents[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new Holder();
            holder.tv = (TextView) convertView.findViewById(R.id.list_txt);
            convertView.setTag(holder);
        } else
            holder = (Holder) convertView.getTag();
        holder.tv.setText(contents[position]);
        return convertView;
    }

    private class Holder {
        TextView tv;
    }

}

package com.sw.gitans201608042027;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class TextFragment extends Fragment {

    private String desc;

    public TextFragment(String s){
        desc = s;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.list_item, container, false);
        ((TextView)v.findViewById(R.id.list_txt)).setText(desc);
        return v;
    }
}

activity_main.xml

    <RelativeLayout
    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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.sw.gitans201608042027.MainActivity" >

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    </RelativeLayout>

list_item.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" >

    <TextView
        android:id="@+id/list_txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

lv.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" >

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>