无法滚动对话框片段 [英] Can't Scroll in Dialog Fragment

查看:61
本文介绍了无法滚动对话框片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弹出一个DialogFragment并显示一个可滚动的TextView图例,但它只是切断了图例的其余部分.我无法滚动浏览它或其他任何内容.我尝试添加ScrollView,但是它什么也没做.这是我得到的屏幕:

I am trying to get a DialogFragment to popup and to show a scrollable TextView legend but it just cuts off the rest of the legend. I can't scroll through it or anything. I tried adding a ScrollView but it doesn't do anything. This is the screen that I get:

这是我的XML布局文件:

This is my XML layout file:

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

I run this to add content to the 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有更多关系.

I want the entire Fragment to be scrollable but don't know how to achieve this. Whenever I try to include a ScrollView, it only scrolls the rest of that specific one line . It won't show all the content. I'm thinking it has more to do with the DialogFragment.

好的,所以我想我知道为什么它不会滚动.它在循环中一遍又一遍地调用View.然后填充层,最后将TextView添加到LinearLayout.因此,它不一定是附加的".因此,添加ScrollView仅会使最后一部分可滚动.如果有人知道如何解决此问题,请告诉我.

Ok, so I think I know why it won't scroll. It calls the View over and over again in the loop. Then it populates the layer, and finally adds the TextView to the LinearLayout. So it is not necessarily being "appended". So adding a ScrollView will only make the last part scrollable. If anyone knows how to fix this, please let me know.

我从这里获得了示例的链接:

I got the link for the example from here:

MapLegend

推荐答案

我想出了如何使整个视图可滚动的方法. ScrollView无法正常工作的原因是由于以下语句:

I figured out how to make the entire view scrollable. The reason the ScrollView was not working was because of this statement:

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

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

It takes the entire layout of that xml and makes it one view. It then loops through the view and creates new views over and over again. So if you wrap the view in a scrollable, it appends the scrollable event to that specific view.

我所做的就是打电话

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布局.

That way, it uses the linearLayout of that xml as opposed to the entire xml layout.

然后我又遇到另一个问题,由于ScrollView,每次将列表压缩到一行.所以我所做的是:

I then ran into another issue with the list being compressed into one line each time because of the ScrollView. So what I did was:

     /**** 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);
    }

在一个视图中显示列表及其内容.

which shows the list with the contents in it in one view.

顺便说一句,我从这个Arshu那里得到了代码,他提供了一个了不起的解决方案.这是链接:

By the way, I got the code from this Arshu who had an amazing solution. Here is the link:

答案

很抱歉,我的回答似乎令人困惑.我对stackoverflow和android环境还很陌生.

I'm sorry if my answer seems confusing. I'm fairly new with stackoverflow and the android enviornment.

这篇关于无法滚动对话框片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆