ListFragment的自定义定位

时间:2014-12-10 06:55:43

标签: android android-layout android-fragments android-listfragment

我有一个Activity,在创建时加载FrameLayout包含SearchViewButton。按钮的onClick方法会在主ListFragment之上加载自定义FrameLayout

我覆盖onCreate的{​​{1}}方法以加载我的自定义ListFragment,并使用我手动创建的字符串数组中的值填充列表。所有这些都能正常工作,但我想在屏幕底部加载ArrayAdapter

我尝试将主ListFragment的重力更改为底部,因为它是FrameLayout的父级,但这会使ListFragmentSearchView降为屏幕底部以及Button

在这个例子中,我在列表中加载了三行。如何向下移动ListFragment,以便屏幕底部只显示第一行,其下方有两行(屏幕外)?

我的MapActivity是主ListFragment

Activity

MapActivity的布局

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MapActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_map);

    }

    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_map, container, false);
        }
    }

    // Define the onShowTimesButtonClick listener
    public void onShowTimesButtonClick(View view){

        // Instantiate timeListFragment
        TimeListFragment timeListFragment = new TimeListFragment();

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.add(R.id.container, timeListFragment, "timeList");
        transaction.commit();
    }
}

TimeListFragment是我的自定义<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MapActivity" tools:ignore="MergeRootFrame" android:clickable="true" android:focusable="false" android:focusableInTouchMode="false"> <SearchView android:layout_width="300dp" android:layout_height="50dp" android:id="@+id/searchBar" android:layout_gravity="start|top" android:layout_marginTop="20dp" android:layout_marginLeft="20dp" android:layout_marginStart="20dp" android:background="#656565" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" android:inputType="text" /> <Button android:id="@+id/timesButton" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_gravity="top|center" android:layout_marginTop="90dp" android:text="@string/times_button" android:onClick="onShowTimesButtonClick"/> </FrameLayout>

ListFragment

TimeListFragment的布局

import android.app.ListFragment;
import android.os.Bundle;
import android.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout.LayoutParams;

/**
 * A simple {@link Fragment} subclass.
 */
public class TimeListFragment extends ListFragment {

    public TimeListFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Temporary time data
        String[] times = new String[]{"1:00", "0:30", "0:45"};

        // Load the time data
        setListAdapter(new TimesListArrayAdapter(getActivity(), R.layout.layout_times_list_row, R.id.listText, times));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_times_list, container, false);

        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.BOTTOM;

        container.setLayoutParams(layoutParams);

        return view;
    }
}

1 个答案:

答案 0 :(得分:0)

这是我到目前为止所拥有的。我可以通过将重力设置为底部将ListFragment移动到屏幕底部。在TimeListFragment的onCreateView方法中,我将layoutParams应用于容器;根据Android文档,此容器是片段的UI附加到的父视图。为了将layoutParams应用于片段本身,我必须首先通过调用膨胀视图上的findViewById来查找片段视图。

onCreateView的正确代码如下所示

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_times_list, container, false);

        View fragmentView = view.findViewById(R.id.timesList);

        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.BOTTOM;

        fragmentView.setLayoutParams(layoutParams);

        return view;
    }