列表片段更改维度



我有一个活动,其线性布局为两个片段(权重 3 和 2(。 第一个片段是列表片段。它最初是其指定的称重高度,但是当列表变大时,它会变得更长并占据整个活动布局,就像我将其高度设置为匹配父级一样。

活动 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/fragment"
android:name="com.example.students.StudentListFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3" />
<fragment
android:id="@+id/fragment2"
android:name="com.example.students.StudentDetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2" />
</LinearLayout>

第一个片段 - 由于它是一个列表片段,我们只有它的java文件。

/**
* A fragment representing a list of Items.
* <p/>
* Activities containing this fragment MUST implement the {@link OnListFragmentInteractionListener}
* interface.
*/
public class StudentListFragment extends ListFragment {
// TODO: Customize parameters
private int mColumnCount = 1;
Context CurrentContext;
LayoutInflater Inflater;
private OnListFragmentInteractionListener mListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = super.onCreateView(inflater,container,savedInstanceState);
Inflater = inflater;
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
recyclerView.setAdapter(new MyStudentRecyclerViewAdapter(DummyContent.ITEMS, mListener));
}

return view;
}

public void onStart()
{
super.onStart();
CurrentContext = Inflater.getContext();
new GetListTask().execute();

}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnListFragmentInteractionListener) {
mListener = (OnListFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnListFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnListFragmentInteractionListener {
// TODO: Update argument type and name
void onListFragmentInteraction(DummyItem item);
}
class GetListTask extends AsyncTask<Void,Void,Object>
{

@Override
protected Object doInBackground(Void[] voids) {
//try
{
SQLhelper helper = new SQLhelper(CurrentContext);
SQLiteDatabase database =  helper.getReadableDatabase();
Cursor cursor;
cursor = database.query("students", new String[]{"_id","USERNAME", "PASSWORD"}, "NOT roll = ?" ,new String[]{"-1"}, null, null, null);
//cursor = database.query("students", new String[]{"_id","USERNAME", "PASSWORD"}, null, null, null, null, null);
return cursor;

}/* catch (Exception e) {
return e;
}*/
}
protected void onPostExecute(Object object) {
if (object instanceof Cursor) {
CursorAdapter listAdapter = new SimpleCursorAdapter(CurrentContext, android.R.layout.simple_list_item_1, (Cursor)object, new String[]{"USERNAME"}, new int[]{android.R.id.text1}, 0) {
};
setListAdapter(listAdapter);
}
else {
new MyToast(CurrentContext, ((Exception)object).getMessage());
}
}
}

}

我希望它保留它的高度,但看起来它不想。

这是你的xml的问题: 您主要在父线性布局中缺少权重总和,因此要解决此问题,只需将权重总和添加到父布局并将片段高度设置为 match_parent:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:weightSum="5"
android:layout_height="match_parent">
<fragment
android:id="@+id/fragment"
android:name="com.example.students.StudentListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3" />
<fragment
android:id="@+id/fragment2"
android:name="com.example.students.StudentDetail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>

最新更新