- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
可能这是一个骗局,但我一直在寻找解决方案,它们总是与我的问题略有不同。
所以:我目前正在创建一个应用程序,其中包含 2 个可滑动的 fragment 。 TaskGroupFragment 显示一个列表,当您单击一个项目时,它会滑动到 TasksFragment 并显示一个子列表。我现在要做的是将所选项目的 ID 从组发送到任务,这样我就可以从 SQLite 中获取子列表。
我知道我应该通过连接的 MainActivity 进行通信,我已经在 TaskGroupsFragment 中创建了一个接口(interface)并在 Activity 中实现了它。经过试验和测试, Activity 收到 TaskGroupID。
我卡住的部分是在 TasksFragment 中获取此信息。尤其是使用 swipeview 会让这更难。
我的代码:
MainPagerAdapter:
public class MainPagerAdapter extends FragmentStatePagerAdapter {
public MainPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0: return TaskGroupFragment.newInstance();
case 1: return TasksFragment.newInstance();
default: return TaskGroupFragment.newInstance();
}
}
@Override
public int getCount() {
return 2;
}
}
TaskGroupActivity(发送 fragment ):
public class TaskGroupFragment extends ListFragment {
private DoItDataSource dataSource;
private List<TaskGroups> groups;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_task_group, container, false);
dataSource = new DoItDataSource(getActivity());
dataSource.open();
JSONContainer jsonContainer = dataSource.sqliteToContainer();
dataSource.close();
groups = jsonContainer.getTask_groups();
TaskGroupAdapter adapter = new TaskGroupAdapter(getActivity(), groups);
setListAdapter(adapter);
return view;
}
public static TaskGroupFragment newInstance() {
TaskGroupFragment tgf = new TaskGroupFragment();
return tgf;
}
public interface OnTaskGroupSelectedListener {
public void onTaskGroupSelected(String taskGroupId);
}
OnTaskGroupSelectedListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnTaskGroupSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " Interface not implemented in activity");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
((MainActivity)getActivity()).setCurrentItem(1, true);
mListener.onTaskGroupSelected(groups.get(position).getId());
}
}
主要 Activity :
public class MainActivity extends FragmentActivity implements
TaskGroupFragment.OnTaskGroupSelectedListener{
private SharedPreferences savedValues;
private DoItDataSource dataSource = new DoItDataSource(this);
private String identifier, user, domain;
private JSONContainer containerToday;
private JSONContainer containerTomorrow;
public ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
savedValues = getSharedPreferences("SavedValues", MODE_PRIVATE);
identifier = savedValues.getString("Identifier", "");
pager = (ViewPager) findViewById(R.id.activity_main_pager);
pager.setAdapter(new MainPagerAdapter(getSupportFragmentManager()));
if (identifier == null || identifier.equals("")) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
intent.putExtra("APP_ID", APP_ID);
startActivity(intent);
}
}
@Override
protected void onResume() {
super.onResume();
identifier = savedValues.getString("Identifier", "");
user = savedValues.getString("User", "");
domain = savedValues.getString("Domain", "");
boolean onBackPressed = savedValues.getBoolean("OnBackPressed", false);
//
// getting lists
//
}
private void resultHandling(String json, String day) {
if (day.equals("today")) {
Gson gson = new Gson();
containerToday = gson.fromJson(json, JSONContainer.class);
jsonToSQLite(containerToday, "Today");
} else if (day.equals("tomorrow")) {
Gson gson = new Gson();
containerTomorrow= gson.fromJson(json, JSONContainer.class);
jsonToSQLite(containerTomorrow, "Tomorrow");
}
}
String taskGroupId = "";
@Override
public void onTaskGroupSelected(String taskGroupId) {
this.taskGroupId = taskGroupId;
// Enter missing link here?
}
}
TaskFragment(接收 fragment ):
public class TasksFragment extends ListFragment
implements OnClickListener {
private final static String TAG = "TaskItemFragment logging";
private DoItDataSource dataSource;
private List<Tasks> tasks;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_task_item, container, false);
Button backButton = (Button)
view.findViewById(R.id.fragment_task_item_bar_back_button);
dataSource = new DoItDataSource(getActivity());
dataSource.open();
tasks = dataSource.getTasks("204"); // 204 is a placeholder, TaskGroupId should be here
dataSource.close();
TasksAdapter adapter = new TasksAdapter(getActivity(), tasks);
setListAdapter(adapter);
backButton.setOnClickListener(this);
return view;
}
public static TasksFragment newInstance() {
TasksFragment tif = new TasksFragment();
return tif;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getActivity(), "Clicked item " + position, Toast.LENGTH_LONG).show();
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.fragment_task_item_bar_back_button:
((MainActivity)getActivity()).setCurrentItem(0, true);
break;
}
}
}
解决方案
感谢 Alireza!我不得不对他提出的代码进行几处更改,但最终它帮助我找到了解决方案!
主页面适配器:
public class MainPagerAdapter extends FragmentStatePagerAdapter {
// ADDED
private String taskGroupId;
public MainPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0: return TaskGroupFragment.newInstance();
// MODIFIED
case 1:
Bundle args = new Bundle();
logcat("before setBundle " + taskGroupId);
args.putString("taskGroupId",taskGroupId);
Fragment fragment = new TasksFragment();
fragment.setArguments(args);
return fragment;
default: return TaskGroupFragment.newInstance();
}
}
// ADDED
public void setTaskGroupId(String id){
this.taskGroupId = id;
}
@Override
public int getCount() {
return 2;
}
}
主要 Activity :
public class MainActivity extends FragmentActivity implements
TaskGroupFragment.OnTaskGroupSelectedListener{
private SharedPreferences savedValues;
private DoItDataSource dataSource = new DoItDataSource(this);
private String identifier, user, domain;
private JSONContainer containerToday;
private JSONContainer containerTomorrow;
// ADDED
private MainPagerAdapter adapter;
public ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
savedValues = getSharedPreferences("SavedValues", MODE_PRIVATE);
identifier = savedValues.getString("Identifier", "");
// ADDED
adapter = new MainPagerAdapter(getSupportFragmentManager());
pager = (ViewPager) findViewById(R.id.activity_main_pager);
// MODIFIED
pager.setAdapter(adapter);
if (identifier == null || identifier.equals("")) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
intent.putExtra("APP_ID", APP_ID);
startActivity(intent);
}
}
@Override
protected void onResume() {
super.onResume();
identifier = savedValues.getString("Identifier", "");
user = savedValues.getString("User", "");
domain = savedValues.getString("Domain", "");
boolean onBackPressed = savedValues.getBoolean("OnBackPressed", false);
//
// Getting lists
//
}
String taskGroupId = "";
@Override
public void onTaskGroupSelected(String taskGroupId) {
this.taskGroupId = taskGroupId;
// ADDED
adapter.setTaskGroupId(taskGroupId);
pager.setAdapter(adapter);
pager.setCurrentItem(1);
}
}
TaskFragment(接收 fragment ):
public class TasksFragment extends ListFragment implements OnClickListener {
private final static String TAG = "TaskItemFragment logging";
private DoItDataSource dataSource;
private List<Tasks> tasks;
private String taskGroupId;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_task_item, container, false);
Button backButton = (Button) view.findViewById(R.id.fragment_task_item_bar_back_button);
dataSource = new DoItDataSource(getActivity());
// ADDED
Bundle bundle = getArguments();
taskGroupId = bundle.getString("taskGroupId");
// MODIFIED
dataSource.open();
tasks = dataSource.getTasks(taskGroupId);
dataSource.close();
TasksAdapter adapter = new TasksAdapter(getActivity(), tasks);
setListAdapter(adapter);
backButton.setOnClickListener(this);
return view;
}
// CAN BE REMOVED?
//public static TasksFragment newInstance() {
// TasksFragment tif = new TasksFragment();
// return tif;
//}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getActivity(), "Clicked item " + position, Toast.LENGTH_LONG).show();
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.fragment_task_item_bar_back_button:
((MainActivity)getActivity()).setCurrentItem(0, true);
break;
}
}
}
请注意,我将 taskGroupId 用作字符串,而不是整数。
最佳答案
首先您需要确保您的适配器知道 taskGroupID。只需向您的适配器添加一个变量和一个公共(public)方法。
public class MainPagerAdapter extends FragmentStatePagerAdapter {
private int taskGroupId;
public void setTaskGroupId(int id){
this.taskGroupId = id;
}
}
然后在您的 Activity 中存储对您的适配器的引用。现在只需在 GroupId 更改时调用此方法
@Override
public void onTaskGroupSelected(String taskGroupId) {
this.taskGroupId = taskGroupId;
adapter.setTastGroupId = taskGroupId; //data needed to initialize fragment.
adapter.setCurrentItem(1); //going to TasksFragment page
}
然后您需要在开始您的 fragment 之前添加一些参数。
@Override
public Fragment getItem(int i) {
//this code is only for case 1:
Bundle args = new Bundle();
args.putInt("taskGroupId",taskGroupId);
Fragment fragment = new TasksFragment();
fragment.setArguments(args);
return fragment;
}
最后在您的 TaskFragment 中使用此数据来显示正确的内容。
关于Android - 使用 swipeview 将数据从一个 fragment 发送到另一个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23368061/
我正在使用这个:https://github.com/nicklockwood/SwipeView 我在我的应用程序的 2 个不同位置使用它。 屏幕上的 30x30 图片大约 100kb 文件大小,这
如果我使用滑动 View ,我会遇到一些问题,并且我编写了以下代码: import QtQuick 2.6 import QtQuick.Window 2.2 import QtQuick.Contr
我正在尝试创建基于 SwipeView 元素的视差 View 。 QML 文档中的示例说明了如何使用 ListView 实现它: Image { id: background sour
我通过 viewpager 有一个带有两个不同 View ( fragment )的 swipeview。我现在想要这两个页面显示来自 sqllite 数据库的不同数据。问题是我无法弄清楚如何做到这一
我将尝试以非常清晰易懂的方式解释我的问题。 我目前在这里使用 SwipeView:http://jasonfry.co.uk/?id=23
我正在使用 jason fry 的 SwipeView,虽然他将它用于 ImageView ,但我正在努力用布局替换它。 目前,如果我用 TextView 替换 ImageView 就可以了,但是我如
我正在使用 Swipeview ( http://cubiq.org/swipeview ) 创建可在 iPhone 和 Android 上运行的触控式 carousel。基础知识非常有用。 当我尝试
我必须在 SwipeView 中使用 QML 组件(例如项目 1、项目 2、项目 3)而不使用 Repeater 来实现页面,并且还需要实现翻页器,如下所示。 getPreviousPage Rect
我正在尝试制作一个带有按钮的滑动 View ,只要单击该按钮,它就会打开第二个 Activity ,但我无法在点击监听器中设置 setActivity 这是我的代码。将进行哪些更改我在我的寻呼机适配器
我的新项目由一个带有 6 个 fragment 的抽屉导航组成(奥迪/宝马/起亚...汽车品牌)。我希望每个 fragment 都有 2 个其他 fragment ( Models ,其中将有一个带有
我是 android 的新手。我正在做简单的 aphabate 应用程序。在那个应用程序中我正在使用字母表字母。当在字母上滑动时它将如何移动到下一个字母。请给我建议 提前致谢 最佳答案 我想建议您查看
我正在使用 iScroll4 在移动网站中的图像上创建水平滚动效果。 iScroll 工作得很好,但问题是 iScroll 包装器中包含的图像禁用了 native 垂直滚动。换句话说,在 iScrol
可能这是一个骗局,但我一直在寻找解决方案,它们总是与我的问题略有不同。 所以:我目前正在创建一个应用程序,其中包含 2 个可滑动的 fragment 。 TaskGroupFragment 显示一个列
我正在努力避免 SideView 和 ListView 似乎都喜欢的这种烦人的重叠。这是一个演示问题的示例: 注意:当您滑动 SwipeView 时请查看左侧的绿色矩形,当您向下滚动 ListView
我使用 swipeview 在两个页面之间滑动,每个页面本身就是一个 Activity (一个 xml 和一个 java 文件)。如果没有 swipeview,我的应用程序可以正常工作,并且我的数据库
我是一名优秀的程序员,十分优秀!