【问题标题】:What's the best way to code with list from other layout?使用其他布局中的列表进行编码的最佳方法是什么?
【发布时间】:2016-04-20 16:08:20
【问题描述】:

我正在尝试向ListView 添加一些数据,其中一个未放置在我当前的布局中,所以我不能使用这个ListView,我正在夸大我的当前 布局与放置ListView 的布局,但我不知道如何使用ListView 进行编码(我无法设置适配器,我无法用这个ListView 做任何事情,显示NullPointerException...)。

我如何使用没有放在我当前的Layout 上的ListView?提前致谢!

更新

布局 A.XML

<?xml version="1.0" encoding="utf-8"?>
<com.github.ksoichiro.android.observablescrollview.ObservableScrollView
android:layout_height="match_parent"
android:id="@+id/observable_scrollview"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"/>

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cv"
        card_view:cardBackgroundColor="@color/cardview_shadow_start_color"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:fitsSystemWindows="true"
        android:isScrollContainer="true"
        card_view:cardCornerRadius="@dimen/cardview_default_radius"
        card_view:cardElevation="@dimen/cardview_default_elevation"
        card_view:cardUseCompatPadding="true">

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="12dp"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingBottom="12dp"
                android:gravity="center"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin">

                <com.bluejamesbond.text.DocumentView
                    card_view:documentView_textColor="@color/circle_color"
                    android:id="@+id/dv"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body2"/>

            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>
    </android.support.v7.widget.CardView>

    <View
        android:id="@+id/view"
        android:layout_width="wrap_content"
        android:layout_height="600dp"
        android:background="@android:color/black" />

</LinearLayout>

MAINACTIVITY.JAVA

@Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.LAYOUT A); //It's not the real name of layout, it's for this question...

    ListView list = (ListView) findViewById(R.id.listViewId);
    cv = (CardView) findViewById(R.id.cv);
    //TextView dv = (TextView) findViewById(R.id.dv);
    DocumentView dv = (DocumentView) findViewById(R.id.dv);
    image = (ImageView) findViewById(R.id.image);
    ObservableScrollView osv = (ObservableScrollView) findViewById(R.id.observable_scrollview);
    osv.setScrollViewCallbacks(this);

    View v = findViewById(R.id.view);
    ViewGroup parent = (ViewGroup) v.getParent();
    v = getLayoutInflater().inflate(R.layout.listview, parent, false);
    int index = parent.indexOfChild(v);
    parent.addView(v, index);
    /*ViewGroup parent = (ViewGroup) findViewById(R.id.parent);
    View C = getLayoutInflater().inflate(optionId, parent, false);
    parent.addView(C, index);*/

    b = getIntent().getExtras();

    ArrayList<String> arrayList = new ArrayList<>();

    for (int x = 0; x < Util.getData().size(); x++) {
        if (b.get(Util.getData().get(x).getNombre()) != null) {
            for (int z = 0; z < Util.GETDATA().get(x).getsomething().size(); z++) {
                arrayList.add(Util.getData().get(x).getsomething().get(z).getname());
            }
        }
    }
    ArrayAdapter adapter = new ArrayAdapter<>(this,
            android.R.layout.simple_list_item_1, arrayList);

        list.setAdapter(adapter); //IT CRASHES HERE!
....

      //I CANNOT USE THIS LIST BECAUSE IT'S ON LAYOUT "B"

布局 B.XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/llListview"
    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=".ChaptersActivity">

    <ListView
        android:focusableInTouchMode="false"
        android:drawSelectorOnTop="true"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/listViewId"
        android:fastScrollEnabled="true"/>

</LinearLayout>

我只是想用 布局 B

扩大 布局 A 的视图

【问题讨论】:

标签: android android-layout listview


【解决方案1】:

你可以试试这个:

LayoutInflater inflater = LayoutInflater.from(this);
View inflatedLayout= inflater.inflate(R.layout.LAYOUT B, null, false);
parent.addView(inflatedLayout);

编辑: 在父级中扩展您的布局后,您可以使用findViewById 获得正确的视图

ListView list = (ListView) parent.findViewById(R.id.listViewId);

【讨论】:

  • 对不起,它不起作用,我可以删除你的代码的第三行添加 parent 而不是 null
  • 它仍然无法正常工作......无论如何,谢谢!我不能像 Google Android Developers 那样做,因为我不使用 appbar、工具栏...
  • 该代码允许您处理 ListView 还是给您一个 nullPointer 异常?
【解决方案2】:

最后,我一直在做这几个步骤来获得我喜欢的解决方案:

  1. CardView 必须退出,因为它是 FrameLayout,我无法将其设置在 ListView 和 ImageView 之间
  2. this place实现 ObservableScrollView
  3. 我想摆脱工具栏,所以我这样做了,因为您可以删除所有工具栏代码(来自 XML 和 Java),它会一如既往地工作!
  4. 一切都很完美,只需要寻找另一种方式在我的布局 A 上实现文本

已解决!

【讨论】:

    猜你喜欢
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多