gpt4 book ai didi

android - Android Studio 中的列表 fragment

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:01:11 24 4
gpt4 key购买 nike

我正在浏览在线教程,试图了解 ListFragment 的工作原理以及我们如何使用它。

教程有点含糊,我无法理解它的确切工作原理,也无法在 Android Studio 上实现 ListFragment。

有人可以提供有关 ListFragment 的工作原理以及我如何在 Android Studio 上实现它的详细信息。

此外,如何在不使用 ListFragment 的情况下在 fragment 中定义 ListView?

最佳答案

关于最后一个问题:

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

我有可能有用的代码。

这是 MainActivity.java,其中包含一个 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:

<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:

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 - Android Studio 中的列表 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29116031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com