如何访问自定义适配器的内部片段的UI元素在Android的一个活动 [英] How can I access UI Elements of a fragment inside of a Custom Adapter From an Activity in Android

查看:199
本文介绍了如何访问自定义适配器的内部片段的UI元素在Android的一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好日子所有,我工作的一个应用程序,我有多个活动(活动A,B,B)的膨胀这个片段(TheListFragment)。

Good day all, I am working on an application where I have multiple Activities (Activities A, B, B) that inflate this fragment (TheListFragment).

这是什么,它​​看起来像一个可视化的再presentation:

This is a visual representation of what it looks like:

目前,我能够从片段本身变化的描述,图像等,但是这不是我想怎么办。我的目标是有各自的活动都可以改变的片段,并使用片段作为可重复使用的列表视图。下面是我的一些code的:

Currently, I am able to change the descriptions, images, etc from the Fragment itself, but that is not what I would like to do. My goal is to have the respective activities all be able to change the fragment and use the fragment as a reusable listview. Here is some of my code:

public class TheListFragment extends Fragment {

    CustomAdapter.Holder holder;

    //The listview
    ListView listView;

    //When the view is created
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //Inflate the add new game layout view
        View view = inflater.inflate(R.layout.list_view_holder, container, false);

        //Setup the listview and reference it to the listview id
        listView = (ListView) view.findViewById(R.id.data_list_view);

        //Set the custom adapter to the listview
        listView.setAdapter(new CustomAdapter(context);

        return view;
    }

    //This custom adapter is used to fill the respective data into the listview
    class CustomAdapter extends BaseAdapter {
        Context context;

        public CustomAdapter(Context context)) {
            this.context = context;
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return all_passed_data.size();
            //List of data. removed it for space purposes
        }

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

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

        //Holds the respective fields
        public class Holder {
            //UI Components
            TextView textview;
            ImageView imageView;

            LinearLayout linearLayout;
        }

        //Custom view
        public View getView(final int position, View convertView, ViewGroup parent) {

            //A holder object to reference the textviews and imageviews
            holder = new Holder();
            View rowView;
            rowView = inflater.inflate(R.layout.custom_list_view, null);

            holder.imageView = (ImageView) rowView.findViewById(R.id.custom_list_view_image);

            holder.textview = (TextView) rowView.findViewById(R.id.custom_list_view_game_name);

            holder.linearLayout = (LinearLayout) rowView.findViewById(R.id.left_layout);

            return rowView;
        }
    }
}

我想设置/改变在活动中被充气片段本身ListView的内容(holder类项目)。 IE浏览器,顺着这个线的东西:

I am trying to set/ alter the listView contents (The holder class items) from within the activity that is inflating the fragment itself. IE, something along the lines of this:

public class ActivityA extends Activity{

    FragmentManager manager;
    ListFragment fragment;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_fragment_holder);

        manager = getFragmentManager();

        ListFragment fragment = (ListFragment) manager.findFragmentById(R.id.the_list_fragment);

        //These will not work at the moment, they read as null pointers
        //This will be changed dynamically depending on the activity. Could be checkbox, imageview, etc.
        fragment.holder.linearLayout.addView(holder.checkBox); 

        fragment.holder.textview.setText("I Want to set the text here");    

        //Make it do something when clicked 
        fragment.holder.textview.setOnClickListener(this); 

        //Use third party library picasso to set the image
        fragment.Picasso.with(context).load("http://www.google.com/somepicture").into(holder.imageview); 

    }
}

但我有没有运气搞清楚如何做到这一点。我已经通过以下链接阅读:从活动通话片段事件,<一个href=\"http://stackoverflow.com/questions/8790426/android-reusing-same-view-object-in-different-activities-the-case-is-about-ad\">Android:在不同的活动重用同一视图对象(此案有关广告横幅),<一个href=\"http://stackoverflow.com/questions/16370426/how-to-access-ui-elements-created-by-a-list-adapter-from-an-activity\">How从一个活动访问由表适配器创建UI元素?。我是否需要实施某种方式访问​​数据的接口?如果是这样,怎么样?或其他方式我失踪?

But I am having no luck figuring out how to accomplish this. I have read through these links: Call fragment events from activity , Android: Reusing same View object in different Activities (the case is about ad banners) , How to access UI elements created by a List Adapter from an Activity? . Do I need to implement an interface that accesses the data somehow? If so, how? Or some other means I am missing?

推荐答案

因此​​,这里是完整的答案,我认为。

So here is the complete answer i think.

首先,让我们每一个类做它的工作。所以活动有片段和片段具有适配器和适配器有持有人。

First let each class do its job. So Activity have Fragments and Fragments have adapters and adapters have Holders.

创建你所需要的方法abatract片段,然后使它们扩大抽象的创建碎片的具体版本。

Create a abatract Fragment with the methods you need and then create concrete version of that fragments by making them extend the abstract one.

在抽象片段的方法之一应该是即公共无效DoSomething的();

one of the methods in the abstract fragment should be i.e. public void doSomething();.

在您的片段的具体版本(A B℃)THA实现你的DoSomething的()方法,实际的东西的方法。在这些framgents使用总汇的某种保持所需的信息(我用一个ArrayList)。这是该列表上的实际数据,并根据需要在屏幕上显示持有人将pupulate。在你的情况下免得假设将放置在屏幕上的元素的文本。这实际上应该是重新presents屏幕即类GameItem具有需要被显示的所有elemtns,甚至复选框状态elemtens一类。那些重新present游戏。

in the concrete version of your fragments (A B o C) tha implements the methods you do the actual "something" on the doSomething() method. in these framgents use some kind of colletion to hold the desired information (i use an arraylist). this is the actual data on the list, and holder will pupulate as needed to show on the screen. in you case lest assume that will be the text to put in the elements on the screen. This actually should be a class that represents the elemtens on the screen i.e. class GameItem that has all the elemtns that need to be shown, even the checkbox state. that represent a game.

然后在你的适配器(自定义适配器),你必须接收两个paramenter的背景和ArrayList中(containg文本的数组或100%正确的应该得到奥运会的数组)从保持活性。此外,在该适配器创建一个名为公共无效changeGameInfo()public方法;在方法是你改变你收到阵列的内容。当您填充列表的持有人将访问请求元素,并抓住此时,相应的数值显示在屏幕上。

Then in you adapter (custom adapter) you have to receive two paramenter the context and the arraylist (containg the array of text or to be 100% correct should receive the array of Games) from the holding activity. Also in this adapter create a public method called public void changeGameInfo(); is in method is where you change the content of the array you receive. The holder will acceess the element when you populate the list and grab the appropiate values to show on the screen.

在你的类创建片段的实例。在正确的时间,你打电话给你的片段中创建方法,i.e.fregment.doSomething();在DoSomething的是你调用adapter.changeGameInfo();因为是保存适配器的一个实例的片段。并且由于适配器有你作为paramters我就知道必须展现elemetn通过游戏ArrayList的一个参考,就是这样,

In your class you create the instance of the fragment. at the correct time you call the method you create within the fragment, i.e.fregment.doSomething(); in the doSomething is where you call the adapter.changeGameInfo(); Since is the fragment that holds an instance of the Adapter. and since the adapter has a reference of the arraylist of games you passed as paramters i will know the elemetn that need to be show and that's it,

因此​​,作为一个简历
XXXX类调用fragment.doSomething()。
在fragment.doSoemthing调用adapter.changeGameInfo()
在适配器你改变paseed作为放慢参数ArrayList的所需游戏重新presentation。
在viewholder只是显示的元素,在更改了数组列表上elemnts记得打电话notifyDataSetChanged();在adpter所以在屏幕上的实际值获取自动更新

So as a resume Class XXXX calls fragment.doSomething(). In fragment.doSoemthing you call adapter.changeGameInfo() in the adapter you alter the desired game representation of the arraylist paseed as paramter. the viewholder just shows the elements, After you change the elemnts on the array list remember to call notifyDataSetChanged(); in the adpter so the actual values on the screen get updated automatically

看viewholder图案的例子

look at examples of the viewholder patter

public class InteractiveArrayAdapter extends ArrayAdapter<Model> {

private final List<Model> list;
private final Activity context;

public InteractiveArrayAdapter(Activity context, List<Model> list) {
  super(context, R.layout.rowbuttonlayout, list);
  this.context = context;
  this.list = list;
}

static class ViewHolder {
  protected TextView text;
  protected CheckBox checkbox;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  View view = null;
  if (convertView == null) {
    LayoutInflater inflator = context.getLayoutInflater();
    view = inflator.inflate(R.layout.rowbuttonlayout, null);
    final ViewHolder viewHolder = new ViewHolder();
    viewHolder.text = (TextView) view.findViewById(R.id.label);
    viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
    viewHolder.checkbox
      .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
          Model element = (Model) viewHolder.checkbox
              .getTag();
          element.setSelected(buttonView.isChecked());

        }
      });
  view.setTag(viewHolder);
  viewHolder.checkbox.setTag(list.get(position));
} else {
  view = convertView;
  ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
} 

这篇关于如何访问自定义适配器的内部片段的UI元素在Android的一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆