gpt4 book ai didi

android - 从自定义基本适配器>getView [包含IMG]单击按钮启动对话框 fragment

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:27:55 24 4
gpt4 key购买 nike

好吧,我有一个显示用户 friend 的列表(这也是一个 fragment 对话框),该列表中的每个项目都有一个按钮(图片中标记为 friend ),当用户单击该按钮时,id 喜欢显示另一个 fragment 显示与该用户交互的所有选项的对话框(好友请求、阻止、发送私有(private)消息等...)问题是这个按钮及其 onClick 监听器当前是通过覆盖我的 ListView 适配器 getView 方法并创建一个 fragmentDialog 来实现的需要访问 fragment 管理器。有什么办法可以做到这一点吗?

编辑:我无法发布项目的实际代码,但我附加了一个简化的基本适配器 w。 onClickListener 应该清楚我想做什么。我无法从基本适配器类访问 fragmentManager 以使对话 fragment 成为可能

LazyAdapter.java
package com.example.androidhive;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LazyAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
return data.size();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
Button requestBtn = (Button)vi.findViewById(R.id.title); // title

HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);

// Setting all values in listview
title.setText(song.get(CustomizedListView.KEY_TITLE));
artist.setText(song.get(CustomizedListView.KEY_ARTIST));
duration.setText(song.get(CustomizedListView.KEY_DURATION));
imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);

requestBtn.setOnClickListener(new myOnClickListener(position));
return vi;
}

public class myOnClickListener implements OnClickListener{
private int position;
private String clicked_uid;
public myOnClickListener(int position){
this.position=position;
}
@Override
public void onClick(View v) {


//THIS IS WHERE IM TRYING TO PUT THE FRAGMENT DIALOG

FragmentManager fm = TabHostFragmentActivity.this.getSupportFragmentManager();
FriendsFamiliarsDialog friendsDialog = new FriendsFamiliarsDialog().newInstance(TabHostFragmentActivity.profile_uid,"friends");
friendsDialog.show(fm, "friendsdialog");





}

}
}

listview fragment dialog getView android

最佳答案

我还努力从适配器访问 fragment 管理器以显示 DialogFragment。这是代码中的解决方案:

public class YourPagerAdapter extends PagerAdapter{

private Context context;

public YourPagerAdapter(Context c) {
this.context = c;
}

@Override
public void onClick(View v) {
FragmentActivity activity = (FragmentActivity)(context);
FragmentManager fm = activity.getSupportFragmentManager();
YourDialogFragment alertDialog = new YourDialogFragment();
alertDialog.show(fm, "fragment_alert");
}
}

诀窍是您将 Activity 作为上下文接收,然后简单地转换它以确保它是 FragmentActivity - 这正是经理所需要的。这是有效的,因为当您在更高级别的文件中调用 YourPagerAdapter 时,您将 Activity 传递给它:

pagerAdapter = new YourPagerAdapter(getActivity());
yourViewPager.setAdapter(pagerAdapter);

关于android - 从自定义基本适配器>getView [包含IMG]单击按钮启动对话框 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18436524/

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