【问题标题】:Android List View with horizontal Layout but Scroll Vertically that replace text view具有水平布局但垂直滚动替换文本视图的 Android 列表视图
【发布时间】:2017-10-15 02:54:37
【问题描述】:

如何在 android 中创建具有水平布局但垂直滚动以替换文本视图的列表视图。 Like this.

实际上我有一个数据库,我正在检索字符串数组中的一列单词。现在我想按顺序打印这个字符串数组。

<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"
tools:context=".MainActivity">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:layout_marginRight="20dp"
    android:layout_marginTop="10dp" />

`公共类 MainActivity 扩展 ActionBarActivity {

private EditText filterText;
private ArrayAdapter<String> listAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView itemList = (ListView)findViewById(R.id.listView);

    DbBackend dbBackend = new DbBackend(MainActivity.this);
    String[] terms = dbBackend.dictionaryWords();

    listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, terms);

    itemList.setAdapter(listAdapter);
    itemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // make Toast when click
            Toast.makeText(getApplicationContext(), "Position " + position, Toast.LENGTH_LONG).show();
            Intent intent = new Intent(MainActivity.this, DictionaryActivity.class);
            intent.putExtra("DICTIONARY_ID", position);
            startActivity(intent);
        }
    });`

【问题讨论】:

  • 请粘贴您的代码
  • 实际上我想要一个列表视图,其中第一项不应断行,其他项应从上一项的末尾开始
  • 用代码编辑你的问题,不要写在cmets中。
  • @Lepidopteron 好的。
  • 请帮助我。非常感谢您提前

标签: android list listview arraylist


【解决方案1】:

根据 Android 文档RecyclerView 是在列表视图中组织项目并水平显示的新方法

优势

1.由于使用Recyclerview Adapter,自动实现ViewHolder pattern

2.动画易于执行

3.更多功能

有关 RecyclerView 的更多信息:

1.grokkingandroid.com

2.androidhive

LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
                mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
                mRecyclerView.setLayoutManager(layoutManager);

【讨论】:

  • RecyclerView 有可能第一项不应断行,其他项应从上一项的末尾开始
  • 你能给我一些关于代码的想法吗?我是安卓新手。我会非常感谢你
  • 请关注 androidhive 链接,它很容易为您服务
【解决方案2】:

您应该考虑使用GridView 或带有GridLayoutManager 的RecyclerView。查看StaggeredGridLayoutManager 将网格包装到项目宽度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 2013-07-08
    • 2015-12-21
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多