ListView项打开一个新片段

时间:2016-08-04 11:44:44

标签: android listview android-fragments android-viewpager

我正在制作一个健身应用程序,我希望有3个嵌套:

  1. @jit(nopython=True) def toto(): a = np.random.randn(20, 10) b = np.random.randn(20) c = np.random.randn(10) d = a - b.reshape((-1, 1)) * c.reshape((1,-1)) return d 肌肉群项目
  2. ListView练习(点击肌肉群中的项目时出现ListView
  3. 和运动的描述。
  4. Something like this design:

    但我不知道怎么意识到这一点。 我是否需要为每个项目创建一个新片段,或者我可以在这里使用ListView(如果是,如何执行此操作)? 请给我建议,如何实现这个设计(相同结构项目的任何链接或任何其他示例) 提前谢谢你

1 个答案:

答案 0 :(得分:0)

以下是示例代码             包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>