无法在对话框片段中滚动

时间:2016-03-28 19:25:00

标签: android android-layout arcgis android-dialogfragment android-scrollview

我正在尝试弹出一个DialogFragment并显示一个可滚动的TextView传奇,但它只是切断了传奇的其余部分。我无法滚动浏览它或任何东西。我尝试添加ScrollView,但它没有做任何事情。这是我得到的屏幕:

enter image description here

这是我的XML布局文件:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:scrollbars="vertical"
        android:scrollbarAlwaysDrawVerticalTrack="true"
        android:background="#fff">

        <TextView
            android:id="@+id/layer_legend_title_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:background="#fff"
            android:textColor="#000" />

        <ListView
            android:id="@+id/layer_legend_symbols_listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:background="#fff"
            android:textColor="#000" />

    </LinearLayout>

</ScrollView>

我运行此操作以向TextView添加内容:

/**
 * A dialog that shows the legend of a ArcGISDynamicMapServiceLayer.
 */
public class LegendDialogFragment extends DialogFragment implements View.OnClickListener {

    public static final String TAG = LegendDialogFragment.class.getSimpleName();
    private LinearLayout mLinearLayout;
    private ArcGISDynamicMapServiceLayer mLayer;
    private ImageButton backButton;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mLinearLayout = (LinearLayout) inflater.inflate(R.layout.legend_dialog_fragment_layout, null);
        getDialog().setTitle(getActivity().getString(R.string.legend));

        mLayer = ((MainActivity) getActivity()).getLayer();

        backButton = (ImageButton) mLinearLayout.findViewById(R.id.backButton2);
        backButton.setOnClickListener(this);
        // before we can show the legend we have to fetch the legend info asynchronously
        new FetchLegendTask().execute();

        return mLinearLayout;
    }

    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (!(activity instanceof MainActivity)) {
            throw new IllegalStateException("Hosting activity needs to be of type MainActivity");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.backButton2:
                getDialog().dismiss();
                break;
        }
    }

    /**
     * Retrieves the legend information asynchronously from the ArcGISDynamicMapServiceLayer.
     */
    private class FetchLegendTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            mLayer.retrieveLegendInfo();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            for (ArcGISLayerInfo layerInfo : mLayer.getAllLayers()) {
                if(layerInfo.isVisible()){
                    View view = getActivity().getLayoutInflater().inflate(R.layout.layer_legend_layout, null);
                    populateLegendView(view, layerInfo);

                    mLinearLayout.addView(view);
                }
            }
        }

        private View populateLegendView(View view, ArcGISLayerInfo layerInfo) {
            if (layerInfo != null) {
                TextView textView = (TextView) view.findViewById(R.id.layer_legend_title_textview);

                textView.setText(layerInfo.getName());
            }
            return view;
        }
    }

}

我希望整个Fragment可以滚动但不知道如何实现这一目标。每当我尝试包含ScrollView时,它只会滚动该特定一行的其余部分。它不会显示所有内容。我认为它与DialogFragment有更多关系。

编辑:好的,所以我想我知道为什么它不会滚动。它在循环中反复调用View。然后它填充图层,最后将TextView添加到LinearLayout。所以它不一定是&#34;附加&#34;。因此,添加ScrollView只会使最后一部分可滚动。如果有人知道如何解决这个问题,请告诉我。

我从这里得到了这个例子的链接:

MapLegend

2 个答案:

答案 0 :(得分:3)

在对话框片段

也许这是微不足道的,但对我来说,它开始工作,在FrameLayout中包装ScrollView之后

在OnCreateView中隐藏DialogFragment

<FrameLayout
   <ScrollView
     <ConstainLayout...>

设定主题

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.RatingDialog2)
    }

样式

 <style name="RatingDialog2" parent="AppTheme">
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowFullscreen">true</item>
            <item name="android:windowIsFloating">true</item>
            <item name="android:windowContentOverlay">@null</item>
            <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
            <item name="android:windowSoftInputMode">adjustPan</item>
            <item name="android:windowBackground">@android:color/transparent</item>
            <item name="android:windowMinWidthMinor">90%</item>
            <item name="android:windowMinWidthMajor">60%</item>
        </style>

答案 1 :(得分:0)

我想出了如何使整个视图可滚动。 ScrollView无法正常工作的原因是因为这句话:

mLinearLayout = (LinearLayout) inflater.inflate(R.layout.legend_dialog_fragment_layout, null);

它采用该xml的整个布局并使其成为一个视图。然后它循环遍历视图并反复创建新视图。因此,如果将视图包装在可滚动的中,它会将可滚动事件附加到该特定视图。

我做的是致电

View view = inflater.inflate(R.layout.legend_dialog_fragment_layout, container, false);
mLinearLayout = (LinearLayout) view.findViewById(R.id.legend_dialog_fragment_linearlayout);

这样,它使用该xml的linearLayout而不是整个xml布局。

然后我遇到了另一个问题,由于ScrollView,列表每次被压缩成一行。所以我做的是:

     /**** Method for Setting the Height of the ListView dynamically.
     **** Hack to fix the issue of not showing all the items of the ListView
     **** when placed inside a ScrollView  ****/
    public void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null)
            return;

        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
        int totalHeight = 0;
        View view = null;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            view = listAdapter.getView(i, view, listView);
            if (i == 0)
                view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, GridLayout.LayoutParams.WRAP_CONTENT));

            view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            totalHeight += view.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

在一个视图中显示包含其内容的列表。

顺便说一下,我从这个Arshu那里得到了一个有着惊人解决方案的代码。这是链接:

Answer

如果我的回答看起来令人困惑,我很抱歉。我对stackoverflow和android环境都很新。