我有一个情况我需要一些建议。我有一个具有可扩展列表视图的应用程序。每个子单击都会将用户发送到下一个活动,即具有列表视图的选项卡布局。选项卡布局有3个选项卡。我试图弄清楚如何在可扩展列表视图中点击子项时将数据发送到3个选项卡列表视图。
最初我打算在setOnChildClickListener中设置它,如下所示:
var assembliesToScane = AppDomain.CurrentDomain.GetAssemblies();
var allTypes = assembliesToScan.SelectMany(a => a.ExportedTypes).ToArray();
var profiles =
allTypes
.Where(t => typeof(Profile).GetTypeInfo().IsAssignableFrom(t.GetTypeInfo()))
.Where(t => !t.GetTypeInfo().IsAbstract);
Mapper.Initialize(cfg =>
{
foreach (var profile in profiles)
{
cfg.AddProfile(profile);
}
});
编辑:这是我的tablayout活动,用于设置3个标签。我不知道在哪里可以访问捆绑的附加内容。
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if (groupPosition == 0) {
if (childPosition == 0) {
mCustomListViewAdapter.addAdapterItem(new CustomObject("Squats", "60%", "6", "150", false));
listViewFri.setAdapter(mCustomListViewAdapter);
我认为它会像这样进入这一部分:
public class WorkoutDaysActivity extends BaseActivity {
ListView listViewFri = (ListView) findViewById(R.id.listViewFri);
ListView listViewMon = (ListView) findViewById(R.id.listViewMon);
ListView listViewWed = (ListView) findViewById(R.id.listViewWed);
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.workout_days);
Bundle extras = getIntent().getBundleExtra("args");
mToolBar = activateToolbarWithHomeEnabled();
setUpNavigationDrawer();
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_workout_days, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
View rootView = inflater.inflate(R.layout.fragment_workout_days, container, false);
Bundle extras = getArguments();
CustomObject objects = (CustomObject) extras.getSerializable("w29w1");
return rootView;
case 2: View rootView2 = inflater.inflate(R.layout.fragment_sub_page1, container, false);
TextView textView2 = (TextView) rootView2.findViewById(R.id.textView2);
textView2.setText("Workout 29 Week 1");
return rootView2;
case 3:
View rootView3 = inflater.inflate(R.layout.fragment_sub_page2, container, false);
TextView textView3 = (TextView) rootView3.findViewById(R.id.txtFrag3);
textView3.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView3;
default: View rootView4 = inflater.inflate(R.layout.fragment_workout_days, container, false);
TextView textView4 = (TextView) rootView4.findViewById(R.id.txtFrag1);
textView4.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView4;
}
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Monday";
case 1:
return "Wednesday";
case 2:
return "Friday";
}
return null;
}
}
}
但是我在(这个,对象)下得到一个错误,说"不能应用于.placeholderfragment .custom object" " PlaceholderFragment无法转换为Context"
这是我在onChildClick中的内容:
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
View rootView = inflater.inflate(R.layout.fragment_workout_days, container, false);
Bundle extras = getArguments();
CustomObject objects = (CustomObject) extras.getSerializable("w29w1");
CustomListViewAdapter customListViewAdapter = new CustomListViewAdapter(this, objects);
感谢您的帮助!如果您需要查看我的任何代码以便更好地了解我想要做的事情,请告诉我们。
答案 0 :(得分:1)
我完全理解这个问题有些困难,但到目前为止,这是我的想法。
您是否尝试在每个列表上设置相同的列表适配器实例?如果您想要相同的一般行为,但要将不同的数据绑定到列表,请确保为每个列表实例化新的适配器实例。
即
myList1.setAdapter(new MyAdapter());
myList2.setAdapter(new MyAdapter());
而不是
MyAdapter myAdapter = new MyAdapter();
myList1.setAdapter(myAdapter);
myList2.setAdapter(myAdapter);
此外,在与其他活动进行通信时,通过意图移动数据是明智的。在您的初始活动中,当某个操作提示启动下一个活动时,请在调用startActivity()
之前将其他内容放入intent中。然后,在下一个活动中,您将调用getIntent().getXXXExtra("...")
,其中XXX
与您提取的额外内容类型匹配,而...
是您放置在额外内容中的正确密钥以前的活动。
如果您正在使用活动中包含的片段,请使用Fragment.setArguments()
(在activity2中)和Fragment.getArguments()
(在fragment2中)将附加内容从fragment1传递到activity2到fragment2。
传递额外内容 当您单击某个项目并想要启动新活动(要显示的新视图)时,一种好方法是捆绑所需的信息,将其传递给新活动,解包所有信息,然后将其应用于视图你渴望。
例如......
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if (groupPosition == 0) {
if (childPosition == 0) {
Bundle extras = new Bundle();
// Put all the extras you need. This can be primitives like int, boolean, double, or complex objects using the Serializable interface
// For example, say your CustomObject implements Serializable
CustomObject customObject = new CustomObject("Squats", "60%", "6", "150", false);
extras.putSerializable("object_key_here", customObject);
Intent intent = new Intent(getContext(), NextActivityHere.class);
intent.putExtra("args", extras);
startActivity(intent);
}
}
在包含包含列表的片段的活动中:
// Likely in onCreate()
Bundle extras = getIntent.getBundleExtra("args");
MyFragment myFragment = new MyFragment();
myFragment.setArguments(extras);
// Use fragment manager to add the fragment to your view
在包含列表的片段(myFragment)中:
// Likely in onCreate()
Bundle extras = getArguments();
CustomObject customObject = (CustomObject) extras.getSerializable("object_key_here");
// Now you can use the object to set up the adapter, etc...
希望这有助于至少显示如何将原始活动中的数据传输到包含列表的片段。通常,如果我要填充列表,我会看到自己传递数据的ArrayList。如果不了解您的情况,就很难更具体。
答案 1 :(得分:1)
带标签的布局
您可能希望对TabLayouts和ViewPagers进行一些阅读。这些是用于创建带选项卡的分页布局的模型。
实现这些视图应该会大大降低Activity的复杂性。在您的活动的onCreate()
方法中,您可以设置TabLayout和ViewPager来托管您的周一,周三和周五片段。
星期一,星期三和星期五片段中的每一个都是相同片段类的实例化,其中包含您要填充的列表(如果我正在获得应用程序布局的正确视图)。
我的假设列表
指导代码(不是所需要的100%)
从XML视图源文件中获取TabLayout和ViewPager视图:
// TabHostActivity.java::onCreate()
setContentView(R.layout.activity_tab_host);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
设置ViewPager:
// TabHostActivity.java::onCreate()
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getFragmentManager());
// Create fragment to populate the tabs
WorkoutDetailFragment mondayFragment = new WorkoutDetailFragment(mondayData);
WorkoutDetailFragment wednesdayFragment = new WorkoutDetailFragment(wednesdayData);
WorkoutDetailFragment fridayFragment = new WorkoutDetailFragment(fridayData);
// Add pages (fragments) to the adapter, passing the fragment and tab title
adapter.addFragment(mondayFragment, "Monday");
adapter.addFragment(wednesdayFragment, "Wednesday");
adapter.addFragment(fridayFragment, "Friday");
// Tie the adapter to your ViewPager
viewPager.setAdapter(adapter);
将ViewPager绑定到TabView:
// TabHostActivity.java::onCreate()
tabLayout.setupWithViewPager(viewPager);
关于实例化新片段的主要内容是...... ViewPager是一个用于保存片段的设备,允许用户在单个活动中更改这些片段。您应该在TabHostActivity.java活动中实例化片段并在ViewPager中设置片段。
上面的示例通过WorkoutDetailFragment构造函数传递列表数据来简化这种情况。这是将数据传递给片段的两种简单方法之一。
通过片段构造函数传递数据:
public class WorkoutDetailFragment extends Fragment {
private ArrayList<Object> listData;
public WorkoutDetailFragment(ArrayList<Object> listData) {
this.listData = listData;
}
// Rest of the code for the fragment...
}
通过片段参数传递数据
// TabHostActivity.java
Bundle args = new Bundle(); // May have received this bundle from extras already. If so, use Bundle args = getIntent.getExtra(...
args.putExtra("list_data", listData); // Where listData is an ArrayList<CustomObject> in your case (Monday/Wednesday/Friday data)
WorkoutDetailFragment fragment = new WorkoutDetailFragment();
fragment.setArguments(args);
// Add the fragment to the view pager...
// Inside WorkoutDetailFragment.java retrieve list data from args using getArguments().getSerializable("list_data")
希望这有帮助!