gpt4 book ai didi

android - 在对话框android中显示我的 fragment

转载 作者:行者123 更新时间:2023-11-30 03:12:54 27 4
gpt4 key购买 nike

我有一个扩展 fragment 的类。我希望在按下 An Activity 中的按钮时打开此 Fragment 及其在对话框中的布局,此外该 fragment 通常会在其位置打开。这可能吗?

这是我的 fragment (TipsFragment.java)

public class TipsFragment extends Fragment{

ListView list;
TipsAdapter tipsAdapter;
AlertDialog PageDialog;
DAO db;
Cursor c;

ArrayList<HashMap<String, String>> tipsList;
HashMap<String, String> map;

ArrayList<HashMap<String, String>> pages;
Integer tipType;

ImageButton page;
ImageButton add;
ImageButton search;

EditText inputAdd;
EditText inputSearch;

ImageView addImage;

RelativeLayout layoutAdd;
RelativeLayout layoutSearch;

int itemSelected;


@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
db = new DAO(activity);
db.open();
}

public TipsFragment(){}

public TipsFragment(int tipType, int itemSelected ){

this.tipType = tipType;
this.itemSelected = itemSelected;
}



@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.tips_fragement, container, false);

System.out.println("tipType: "+tipType);
System.out.println("itemSelected: "+itemSelected);

page = (ImageButton) rootView.findViewById(R.id.pager);
add = (ImageButton) rootView.findViewById(R.id.add);
search = (ImageButton) rootView.findViewById(R.id.search);

inputSearch = (EditText) rootView.findViewById(R.id.inputSearch);
inputAdd = (EditText) rootView.findViewById(R.id.inputAdd);

addImage = (ImageView) rootView.findViewById(R.id.imageAdd);

layoutAdd = (RelativeLayout) rootView.findViewById(R.id.layoutAdd);
layoutSearch = (RelativeLayout) rootView.findViewById(R.id.layoutSearch);

if (tipType != 0) {
switch (tipType) {
case 1:
System.out.println("All tips");
if (getActivity().getIntent().getStringExtra("startFrom") == null) {
c = db.getTips("0");
} else {
c = db.getTips(getActivity().getIntent().getStringExtra("startFrom"));
}

break;
case 2:
System.out.println("favorite tips");
c = db.getFavoriteTips();
page.setVisibility(View.GONE);
System.out.println("in favorite, count_records: "+c.getCount());
break;

}
}

System.out.println("count_records: "+c.getCount());

if (c.getCount() != 0) {

if (getActivity().getIntent().getStringExtra("startLabel") != null) {
page = (ImageButton) rootView.findViewById(R.id.pager);
}
tipsList = new ArrayList<HashMap<String, String>>();

list = (ListView) rootView.findViewById(R.id.tipList);

do {
map = new HashMap<String, String>();

// adding each child node to HashMap key =&gt; value
map.put(DAO.TIP_ID, c.getString(c.getColumnIndex(c.getColumnName(0))));
map.put(DAO.TIP_CONTENT, c.getString(c.getColumnIndex(c.getColumnName(1))));

// adding HashList to ArrayList
tipsList.add(map);

} while (c.moveToNext());

// Getting adapter by passing xml data ArrayList
tipsAdapter = new TipsAdapter(getActivity(), tipsList);
list.setAdapter(tipsAdapter);

// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {

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

map = tipsList.get(position);

System.out.println("in list select item, tipType: "+tipType);

if (!MainActivity.tSlidingLayer.isOpened()) {
MainActivity.tSlidingLayer.openLayer(true);
}


}
});

page.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Strings to Show In Dialog with Radio Buttons


}
});

add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {


}
});

search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {


}
});

inputSearch.addTextChangedListener(new TextWatcher() {

public void afterTextChanged(Editable s) {

}

public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

public void onTextChanged(CharSequence s, int start, int before, int count) {

}
});

}


addImage.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "addImage pressed", Toast.LENGTH_LONG).show();
}
});

return rootView;
}

这是布局 tips_fragement.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"

android:orientation="vertical" >

<LinearLayout
android:id="@+id/titleBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:orientation="horizontal" >

<ImageButton
android:id="@+id/add"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:src="@drawable/add" />

<ImageButton
android:id="@+id/search"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:src="@drawable/search" />

<ImageButton
android:id="@+id/pager"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:src="@drawable/pager" />

</LinearLayout>

<View
android:layout_width="fill_parent"
android:layout_height="5dip"
android:background="@drawable/shadow" >
</View>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layoutAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" >

<EditText
android:id="@+id/inputAdd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Add" />

<ImageView
android:id="@+id/imageAdd"
android:padding="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/inputAdd"
android:layout_alignBottom="@+id/inputAdd"
android:layout_alignRight="@+id/inputAdd"
android:src="@android:drawable/ic_menu_add" />

</RelativeLayout>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/layoutSearch"
android:visibility="gone" >

<EditText
android:id="@+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search"/>

<ImageView
android:id="@+id/imageSearch"
android:padding="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/inputSearch"
android:layout_alignBottom="@+id/inputSearch"
android:layout_alignRight="@+id/inputSearch"
android:src="@android:drawable/ic_menu_search" />

</RelativeLayout>

<ListView
android:id="@+id/tipList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/buttons"
android:divider="#351802"
android:dividerHeight="2dp"
android:listSelector="@drawable/list_selector" />

</LinearLayout>

最佳答案

由于 Android 支持 nesting Fragments(在支持库中可用)您可以将您的 TipsFragment 显示为另一个 DialogFragment 的内容。尽可能避免使用此解决方案。

更好的解决方案是使 TipsFragment 扩展 DialogFragment这样您就可以内联或在对话框中显示 Fragment

1 - 使 TipsFragment 扩展 DialogFragment

public class TipsFragment extends DialogFragment {...}

2 - 在按钮上单击使用 FragmentTransaction.addFragmentTransaction.replace显示 DialogFragment 内联或将其包含在您的布局中并更改其可见性。

3 - 将 TipsFragment 显示为对话框

TipsFragment fr = new TipsFragment();
fr.show(getSupportFragmentManager(), "dialog_fragment_tag");


指南:Performing Fragment Transactions .来自 SDK samplesApiDemos 应用程序中提供了更多示例

关于android - 在对话框android中显示我的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20634377/

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