gpt4 book ai didi

java - ListFragment 更改尺寸

转载 作者:行者123 更新时间:2023-12-02 01:45:36 24 4
gpt4 key购买 nike

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

Activity 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>

第一个 fragment - 因为它是一个 listFragment,所以我们只有它的 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 中:您主要缺少父线性布局中的权重总和,因此要解决此问题,只需将权重总和添加到父布局中并将 fragment 高度设置为 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>

关于java - ListFragment 更改尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57459464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com