好日子,我正在研究一個應用程序,其中有多個活動(活動A,B,B) TheListFragment)。如何從Android中的活動訪問自定義適配器內的片段的UI元素
這是什麼樣子的可視化表示:
目前,我能夠從片段本身發生變化的說明,圖像等,但是這不是我想要做什麼。我的目標是讓各個活動都能夠更改片段並將片段用作可重用的列表視圖。下面是我的一些代碼:
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類項目)。即沿此線的東西:
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);
}
}
,但我有沒有運氣搞清楚如何做到這一點。我已閱讀這些鏈接: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?。我需要實現一個以某種方式訪問數據的接口嗎?如果是這樣,怎麼樣?或者我缺少一些其他方法?
正如一個建議,實例**不要**做到這一點。只有片段應該訪問/更改持有者的內容。您需要一個抽象片段,然後是一個具體版本的片段,即(片段A,B o C),它實現了抽象片段的抽象方法,並根據需要使用該活動來創建應用片段。我希望你能在這裏解釋我想要解釋的。 – Gatunox 2015-02-11 23:34:59
從其他方面說起來有道理,那麼,有沒有辦法將請求從活動發送到碎片產生實際機會的碎片? IE,如果我從活動A發送,[Textview A應該是「ff7」,imageViewA應該是「圖片」,LinearLayoutA應該變成一個複選框並且被選中],將它發送到片段,然後讓片段實際進行更改? – Silmarilos 2015-02-11 23:43:03
當然,只需在片段內創建一個公共方法,比如'public void doSomething()',並且在創建具體片段後從該活動調用該方法即。 'Fragment fragment = new SomeFragment();'然後'fragmemt.doSomething;'在活動 – Gatunox 2015-02-11 23:47:02