繁体   English   中英

用其他布局中的列表编码的最佳方法是什么?

[英]What's the best way to code with list from other layout?

我想一些数据添加到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>

维护性

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

我只想将布局A的视图与布局B结合起来

您可以尝试以下方法:

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);

最后 ,我已经执行了以下几步以获得我喜欢的解决方案:

  1. CardView必须掉了,因为它是一个FrameLayout,我不能在ListView和ImageView之间的中间设置它
  2. 这个地方实现ObservableScrollView
  3. 我想摆脱工具栏,所以我做到了,因为您可以删除所有工具栏代码(从XML和Java),它将像以往一样工作!
  4. 一切工作正常,只需要寻找另一种方法在我的布局A上实现文本

解决了!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM