我正在使用自定義適配器來製作包含文本和按鈕的自定義列表。 現在我需要在一個片段中看到這個視圖,這樣我可以輕鬆地在兩個不同視圖之間切換。 但我似乎無法弄清楚如何在片段代碼中添加活動。是否因爲我最終在另一個適配器內部安裝了適配器?如何讓自定義列表適配器在片段適配器內部工作?
編輯:我的applicantsRequestActivity是測試自定義列表的功能工作,我只需要將其移入片段。
我的主要活動
Activity (Label = "Main")]
public class Test : FragmentActivity
{
private ViewPager mViewPager;
private SlidingTabScrollView mScrollView;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.FragMain);
mScrollView = FindViewById<SlidingTabScrollView> (Resource.Id.sliding_tabs);
mViewPager = FindViewById<ViewPager> (Resource.Id.viewPager);
// setup mViewPager
mViewPager.Adapter = new ApplicantsAdapter (SupportFragmentManager);
mScrollView.ViewPager = mViewPager;
}
}
我的片段
public class ApplicantsAdapter : FragmentPagerAdapter
{
private List<Android.Support.V4.App.Fragment> mFragmentHolder;
public applicantsReqAdapter (Android.Support.V4.App.FragmentManager fragManager) : base (fragManager)
{
mFragmentHolder = new List<Android.Support.V4.App.Fragment>();
mFragmentHolder.Add (new Applicants());
mFragmentHolder.Add (new Acceptes());
}
public override int Count {
get { return mFragmentHolder.Count; }
}
public override Android.Support.V4.App.Fragment GetItem (int position)
{
return mFragmentHolder [position];
}
}
public class Applicants : Android.Support.V4.App.Fragment
{
private EditText mTxt;
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate (Resource.Layout.Frag2Layout, container, false);
mTxt = view.FindViewById<EditText> (Resource.Id.editText1);
mTxt.Text = "Fragment 1 Class :)";
return view;
}
public override string ToString() //Called on line 156 in SlidingTabScrollView
{
return "Fragment 1";
}
}
public class Acceptes : Android.Support.V4.App.Fragment
{
private EditText mTxt;
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate (Resource.Layout.Frag2Layout, container, false);
mTxt = view.FindViewById<EditText> (Resource.Id.editText1);
mTxt.Text = "Fragment 2 Class :)";
return view;
}
public override string ToString() //Called on line 156 in SlidingTabScrollView
{
return "Fragment 2";
}
}
這裏是什麼,我需要爲第一個片段
[Activity (Label = "Applicants")]
public class applicantsRequestActivity : Activity
{
DatabaseAccess db = new DatabaseAccess();
static ListView applicantsListView;
static applicantsRequestAdapter listAdapter;
static List<applicantsRequestList> applicantsList = new List<applicantsRequestList>();
//private ViewPager mViewPager;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
}
public void GetApplicants()
{
/*
get the list from the database
*/
applicantsList.Add (new PartyRequestList() { applicantsId = 1, Name = "James"});
applicantsList.Add (new PartyRequestList() { applicantsId = 2, Name = "Thomas"});
}
bool isDownloaded = false;
protected override void OnStart()
{
base.OnStart();
applicantsListView = FindViewById<ListView> (Resource.Id.PRR_listView);
listAdapter = new applicantsRequestAdapter (this, applicantsList);
applicantsListView.Adapter = listAdapter;
if (!isDownloaded) {
ThreadPool.QueueUserWorkItem (o => GetApplicants());
isDownloaded = true;
}
}
public class PartyRequestList
{
public int applicantsId { get; set; }
public string Name { get; set; }
}
public class applicantsRequestAdapter : BaseAdapter<PartyRequestList>
{
private Button RemoveButton;
private Button AcceptButton;
private readonly IList<PartyRequestList> _items;
private readonly Context _context;
public applicantsRequestAdapter (Context context, IList<PartyRequestList> items)
{
_items = items;
_context = context;
}
public override long GetItemId (int position)
{
return position;
}
public override int Count {
get { return _items.Count; }
}
public override applicantsRequestList this [int position] {
get { return _items [position]; }
}
public override View GetView (int position, View convertView, ViewGroup parent)
{
var item = _items [position];
var view = convertView;
if (view == null) {
var inflater = LayoutInflater.FromContext (_context);
view = inflater.Inflate (Resource.Layout.applicantsRequestRow, parent, false);
RemoveButton = view.FindViewById<Button> (Resource.Id.PRR_Remove);
AcceptButton = view.FindViewById<Button> (Resource.Id.PRR_Accept);
RemoveButton.Tag = position;
AcceptButton.Tag = position;
RemoveButton.Click += RemoveButton_Click;
AcceptButton.Click += AcceptButton_Click;
}
view.FindViewById<TextView> (Resource.Id.PRR_Name).Text = item.Name;
return view;
}
void RemoveButton_Click (object sender, EventArgs e)
{
var pos = (int)((Button)sender).Tag;
PartyRequestList _aa = applicantsList [pos];
Toast.MakeText (_context, (String.Format ("Remove: {0} {1}", _aa.Name, pos)), ToastLength.Short).Show();
applicantsList.RemoveAt (pos);
NotifyDataSetChanged();
}
void AcceptButton_Click (object sender, EventArgs e)
{
var pos = (int)((Button)sender).Tag;
PartyRequestList _aa = applicantsList [pos];
Toast.MakeText (_context, (String.Format ("Accept: {0} {1}", _aa.Name, pos)), ToastLength.Short).Show();
NotifyDataSetChanged();
}
}
}
視圖主要axml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E2E2E2"
android:id="@+id/sample_main_layout">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />
<SlidingTabLayout.SlidingTabScrollView
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
2 axml我用我的自定義列表
名單axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/PRR_listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
行axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/PRR_Name"
android:layout_height="wrap_content"
android:layout_width="0px"
android:layout_weight="1"
android:text="Name" />
<Button
android:text="Remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/PRR_Remove"
android:tag="Remove" />
<Button
android:text="Accept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/PRR_Accept"
android:tag="Accept" />
對不起,如果不清楚。我做了測試自定義列表的活動。我只需要片段中活動的功能。我現在不會如何在fragmet適配器中添加自定義適配器。 –
希望我在這裏提供幫助。我正在處理一個非常類似的問題。 – thurst0n