ListFragment在Android的工作室 [英] ListFragment in Android Studio

查看:117
本文介绍了ListFragment在Android的工作室的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是通过教程去网上尝试理解ListFragment如何工作以及我们如何使用它。

I was going through tutorials online trying to understand how a ListFragment works and how do we use it.

该教程是有点含糊,我不明白它是如何工作究竟也不是我能够实现在Android Studio中ListFragment。

The tutorials were a bit vague and I could not understand how it works exactly nor was I able to implement a ListFragment on Android Studio.

可能有人请提供关于如何ListFragment工作以及如何实现它在Android Studio的详细信息。

Could someone please provide details on how a ListFragment works and how do I implement it on Android Studio.

另外,如何定义一个片段里面一个ListView不使用ListFragment?

Also, how do I define a ListView inside a fragment without using ListFragment?

推荐答案

关于最后一个问题:

另外,如何定义一个片段里面一个ListView不使用ListFragment?

Also, how do I define a ListView inside a fragment without using ListFragment?

我有code,可以帮助。

I have code that might help.

下面是MainActivity.java,它包括一个片段:

Here is the MainActivity.java, which includes a Fragment:

    public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new MusicFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent i = new Intent(this, SettingsActivity.class);
            startActivity(i);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class MusicFragment extends Fragment {

        ListView list;
        String[] text = { "House of Whispers","Hot Lunch", "Number of the Beast", "Killers"};
        Integer[] imageId = { R.drawable.hotlunch1, R.drawable.hotlunch2,
                R.drawable.ironmaiden1, R.drawable.ironmaiden2 };

        public MusicFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            list = (ListView) rootView.findViewById(R.id.ListView);
            CustomAdapter adapter = new CustomAdapter(getActivity() , text, imageId );
            list.setAdapter(adapter);

            Log.d("CustomAdapter", "MusicFragment onCreateView successful");

            return rootView;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);


        }

    }
}

fragment_main.xml:

fragment_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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$PlaceholderFragment">

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

</RelativeLayout>

编辑:根据要求,这里是CustomAdapter:

As requested, here is the CustomAdapter:

public class CustomAdapter extends ArrayAdapter<String> {
    private final Activity _context;
    private final String[] _text;
    private final Integer[] _imageId;

    public CustomAdapter(Activity context, String[] text, Integer[] imageId) {
        super(context, R.layout.list_item, text);
        this._context = context;
        this._text = text;
        this._imageId = imageId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = _context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_item, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.text);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        txtTitle.setText(_text[position]);
        imageView.setImageResource(_imageId[position]);

        return rowView;
    }

}

此外,这里是整个Android的Studio项目在GitHub上: https://github.com/dmnugent80/CustomAdapter

Also, here is the entire Android Studio project on GitHub: https://github.com/dmnugent80/CustomAdapter

这篇关于ListFragment在Android的工作室的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆