FragmentDialog内的ViewPager高度始终等于0

时间:2014-01-14 00:14:51

标签: android android-fragments android-viewpager android-support-library android-dialogfragment

我知道很多问题就像这个问题一样,但我没有看到与此案有任何真正的相似之处。

我正在使用android android.support.v4.app.DialogFragment和android.support.v4.app.Fragment

我正在尝试在包含滑动视图的弹出视图中显示网络服务响应,所以我使用的是DialogFragment,里面是ViewPager,当然还有{{1}在ListView中的每个片段内显示结果。

  

代码:显示对话框的活动

ViewPager
  

ResultFragmentDialog代码:

new ResultFragmentDialog().show(getSupportFragmentManager(), TAG_RESULT_DIALOG_FRAGMENT);
  

ResultListDialogFragment代码:

public class ResultFragmentDialog extends DialogFragment {

private View view;
private ViewPager pager;
private FragmentsResultAdapter adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.dialog_result, container, false);
    Dialog dialog = getDialog();
    dialog.setOnShowListener(new OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {
//Im creating the adapter here because if I don't there will be an IllegalArgumentException: No view found for id for fragment ..
            pager = (ViewPager) view.findViewById(R.id.pager);
            adapter = new FragmentsResultAdapter(getChildFragmentManager());
            pager.setAdapter(adapter);
        }
    });
    return view;
}

private class FragmentsResultAdapter extends FragmentStatePagerAdapter {

    private ResultListDialogFragment firstFragment;
    private ResultListDialogFragment secondFragment;

    public FragmentsResultAdapter(FragmentManager fm) {
        super(fm);
        firstFragment = new ResultListDialogFragment();
        secondFragment = new ResultListDialogFragment();
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return false;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return position == 0 ? "First" : "Second";
    }

    @Override
    public Fragment getItem(int position) {
        return position == 0 ? firstFragment : secondFragment;
    }

    @Override
    public int getItemPosition(Object object){
        return PagerAdapter.POSITION_NONE;
    }
}
}
  

布局文件中的ViewPager代码:

public class ResultListDialogFragment extends Fragment {

private View view;
private ListView list;
private ResultAdapter adapter;

private ResponseMultipleItems result = new ResponseMultipleItems(); 

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_dialog_result_lst, container, false);
    list = (ListView) view.findViewById(R.id.lstResult);
    list.setOnItemClickListener(adapter);
    adapter = new ResultAdapter(getActivity());
    list.setAdapter(adapter);
    return view;
}

public void loadResult(List<Item> result) {
    result.setResult(result);
    adapter.notifyDataSetChanged();
}

public void clear() {
    result.getResult().clear();
    notifyDataSetChanged();
}

private class ResultAdapter extends BaseAdapter implements OnItemClickListener {

    private Context context;

    private ResultItem item;
    private Holder holder;

    public ResultAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return result.getResult().size();
    }

    @Override
    public Object getItem(int position) {
        return result.getResult().get(position);
    }

    @Override
    public long getItemId(int position) {
        return result.getResult().get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        item = result.getResult().get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_result, parent, false);
            holder = new Holder();
            holder.txt = (TextView) convertView.findViewById(R.id.txtTitle);
            convertView.setTag(holder);
        }
        holder = (Holder) convertView.getTag();
        if (position == result.getResult().size() - 1 && result.isHasMore()) {
            holder.txt.setText("Load more");
        } else {
            holder.txt.setText(item.getTitle());
        }
        return convertView;
    }

    private class Holder {
        TextView txt;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
}
  

布局文件中的ListView代码:

 <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>

最后问题是片段中创建的ListView的高度为<ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/lstResult" android:layout_width="match_parent" android:layout_height="match_parent" /> !!!

我怎么知道两个片段中的列表的高度为零?我在开始时并不知道,但0内的public View getView(int position, View convertView, ViewGroup parent)未被调用,而ResultAdapter所有方式在调用notifyDataSetChanged()之后被调用。

所以我使用了HeirarchyViewer,它显示public int getCount()具有正常高度,来自heirarchyviewer的每个片段DialogFragment attr的NoSaveStateFrameLayout显示getHeight()0 1 {} ListView内部也有NoSaveStateFrameLayout高度。

对话框显示成功,我可以在两个不可见的视图之间滑动! 我没有成功写入的后台代码获取内容并将它们传递给适配器,但0未被触发!!

  • 尝试使用public View getView(int position, View convertView, ViewGroup parent)代替wrap_content无效。
  • 在创建列表视图后尝试match_parent也无效。
  • 尝试使用新的宽度和高度设置新的getLayoutParams().height = 100,但也无效。
  • 尝试使用LayoutParam代替Dialog也无效。

0 个答案:

没有答案