gpt4 book ai didi

android - 如何设置 fragment 对话框大小以换行到 ListView

转载 作者:太空宇宙 更新时间:2023-11-03 10:40:58 25 4
gpt4 key购买 nike

我有 RecyclerView,当我长按其中一项时,会出现带有自定义布局的 FragmentDialog:|@ text|。问题是 Fragment Dialog 不只将区域包裹到这 2 个元素,而是占据了几乎整个屏幕的位置。即使我将布局设置为包装内容,问题仍然出现。这是问题所在: enter image description here

这是 RecyclerView custom_item XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|left"
android:padding="8dp"
android:orientation="horizontal"
android:clickable="true"
android:background="?android:attr/selectableItemBackground">

<!-- Option Icon -->
<ImageView
android:id="@+id/recipe_dialog_option_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="@color/primary"
android:layout_marginRight="4dp"
android:src="@drawable/ic_email_48dp" />

<!-- Text Option -->
<TextView
android:id="@+id/recipe_dialog_option_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@color/primary_text"
android:text="Dodaj do Ulubionych" />

</LinearLayout>

这是 FragmentDialog 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<android.support.v7.widget.RecyclerView
android:id="@+id/dialog_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

和 .class 以防万一:

package com.example.nazwamarki.myapplication.recipedialog;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.example.nazwamarki.myapplication.R;

import java.util.ArrayList;

public class RecipeDialogFragment extends DialogFragment {

private ArrayList<RecipeDialogItem> recipeDialogItems;
private RecipeDialogAdapter recipeDialogAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

return inflater.inflate(R.layout.dialog_fragment, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
recipeDialogItems = new ArrayList<>();
RecyclerView dialogRecyclerView = (RecyclerView) view.findViewById(
R.id.dialog_recycler_view);

fillRecipeDialogArray(recipeDialogItems);

recipeDialogAdapter = new RecipeDialogAdapter(getContext(), recipeDialogItems,
R.layout.recipe_dialog_item);

dialogRecyclerView.setHasFixedSize(true);
dialogRecyclerView.setAdapter(recipeDialogAdapter);
dialogRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}

private void fillRecipeDialogArray(ArrayList<RecipeDialogItem> recipeDialogitem) {
RecipeDialogItem dialogItem;

String[] names = new String[] {"Account", "Star"};
int[] icons = new int[] {R.drawable.ic_account_plus_48dp, R.drawable.ic_account_star_48dp};

for (int i = 0; i < 2; i++) {
dialogItem = new RecipeDialogItem();
dialogItem.setRowIcon(icons[i]);
dialogItem.setRowOption(names[i]);
recipeDialogitem.add(dialogItem);
}
}
}

最佳答案

ListView 宽度传递给 DialogFragment 然后在 DialogFragmentonStart() 中执行以下操作

@Override
public void onStart()
{
super.onStart();

// safety check
if (getDialog() == null)
return;

int dialogWidth = getArguments().getInt("width"); // specify a value here
int dialogHeight = ... // specify a value here

getDialog().getWindow().setLayout(dialogWidth, dialogHeight);

// ... other stuff you want to do in your onStart() method
}

Showing DialogFragment 的最佳实践

private void showDialog() {
// DialogFragment.show() will take care of adding the fragment
// in a transaction. We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
transaction.remove(prev);
}
transaction.addToBackStack(null);

// Create and show the dialog.
DialogFragment newFragment = RecipeDialogFragment.newInstance(width);
newFragment.show(transaction, "dialog");
}

RecipeDialogFragment

    /**--Static factory method---
* Create a new instance of RecipeDialogFragment, providing "width"
* as arguments.
*/
static RecipeDialogFragment newInstance(int width) {
RecipeDialogFragment fragment = new RecipeDialogFragment();
Bundle args = new Bundle(1);
args.putInt("width", width);
fragment.setArguments(args);
return fragment;
}

关于android - 如何设置 fragment 对话框大小以换行到 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33903719/

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