- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我在 TabLayout 中重复的 fragment 中有一个 RecyclerView
。我在 RecyclerView 中遇到了 View 未更改的问题。我在每个选项卡上都有一个微调器。我想在选择微调器项目时更改数据。
My cases:
- when switching between tabs - items changed
when selecting another value in the spinner in the first tab -items not changed. (but data is changing in adapter class.Ie. First, it is not null then null during selection. But the first not nulled data is not appearing, its replacing with null. Found it using breakpoints).
Note: In this case, when switching the tab, the items get changed to the spinner selected items in the previous tab. And then it disappears and displaying the current items in the tab.
when selecting another value in the spinner in the last tab -items changed.
我的 View 寻呼机适配器类
public class StudentViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<StudentList> mFragmentList = new ArrayList<>();
private final List<Clazz> mFragmentTitleList = new ArrayList<>();
public StudentViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(StudentList fragment,Clazz clazz){
mFragmentList.add(fragment);
mFragmentTitleList.add(clazz);
}
@Override
public Fragment getItem(int position) {
return StudentList.newInstance(mFragmentTitleList.get(position));
}
@Override
public int getCount() {
return mFragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position).getName();
}
}
我的 RecyclerView
适配器类
public class PeopleAdapter extends RecyclerView.Adapter<PeopleAdapter.MyViewHolder> implements View.OnClickListener {
private List<Student> dataList;
private Context context;
private Clicker clicker;
public PeopleAdapter(List<Student> data, Context context, Clicker clicker) {
this.dataList = data;
this.context = context;
this.clicker = clicker;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.people_list_item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Student data=dataList.get(position);
holder.email.setText(data.getEmail());
holder.name.setText(data.getName());
holder.phone.setText(data.getPhone());
Glide.with(context).load(Method.getImageUrl(MyConfiguration.STUDENT_IMAGE_URL,
data.getStudentId())).asBitmap().into(holder.profilePic);
holder.edit.setOnClickListener(this);
holder.edit.setTag(position);
}
@Override
public int getItemCount() {
return dataList.size();
}
@Override
public void onClick(View v) {
clicker.OnItemClicked((int) v.getTag(),null);
}
static class MyViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.name)
TextView name;
@BindView(R.id.email)
TextView email;
@BindView(R.id.phone)
TextView phone;
@BindView(R.id.image)
ImageView profilePic;
@BindView(R.id.imageedit)
ImageView edit;
MyViewHolder(View view) {
super(view);
ButterKnife.bind(this,view);
}
}
}
标签 fragment
public class StudentList extends Fragment implements SectionChanger {
@BindView(R.id.studentlist)
RecyclerView mRecyclerview;
CompositeDisposable disposable;
private Unbinder unbinder;
private Clazz clazz;
private Requester requester;
public StudentList() {
StudentInformation.bindSectionChangeListener(this);
}
public static StudentList newInstance(Clazz clazz) {
StudentList fragment=new StudentList();
Bundle args = new Bundle();
args.putSerializable(MyConfiguration.SECTIONS, clazz);
fragment.setArguments(args);
return fragment;
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
disposable.clear();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_student_list, container, false);
unbinder = ButterKnife.bind(this, view);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerview.setLayoutManager(mLayoutManager);
initializeRetrofit();
if (getArguments() != null) {
clazz = (Clazz) getArguments().getSerializable(MyConfiguration.SECTIONS);
loadStudentJson(clazz != null ? clazz.getClassId() : null,
clazz != null ? clazz.getSections().get(0).getSectionId() : null);
}
return view;
}
/**
* Load students list
*/
public void loadStudentJson(String class_id,String section_id) {
disposable = new CompositeDisposable(requester.getStudentsInSection(class_id,section_id)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(
this::handleResponse,
this::handleError
)
);
}
private void handleResponse(List<Student> list) {
PeopleAdapter adapter=new PeopleAdapter(list, getActivity(),
(position, name) -> Toast.makeText(getActivity(), position, Toast.LENGTH_LONG).show());
mRecyclerview.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
private void handleError(Throwable error) {
Toast.makeText(getActivity(), "Error " + error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public void ChangeData(Section section) {
loadStudentJson(section.getClassId(),section.getSectionId());
}
public void initializeRetrofit(){
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
CookieHandler handler=new Cookies(getActivity());
//ClearableCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getActivity()));
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.cookieJar(new JavaNetCookieJar(handler))
.build();
requester = new Retrofit.Builder()
.baseUrl(MyConfiguration.BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(JacksonConverterFactory.create())
.client(client)
.build().create(Requester.class);
}
}
Viewpager 设置 fragment
public class StudentInformation extends Fragment implements TabLayout.OnTabSelectedListener, AdapterView.OnItemSelectedListener {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private Unbinder unbinder;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
CompositeDisposable disposable;
List<Section> sectionsList=new ArrayList<>();
private OnConnectingFragments mListener;
@BindView(R.id.tabs)
TabLayout mTabLayout;
@BindView(R.id.viewpager)
ViewPager viewPager;
@BindView(R.id.secSelector)
Spinner spinner;
@BindView(R.id.className)
TextView className;
private static SectionChanger sectionChanger;
public StudentInformation() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment StudentInformation.
*/
// TODO: Rename and change types and number of parameters
public static StudentInformation newInstance(String param1, String param2) {
StudentInformation fragment = new StudentInformation();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
disposable.clear();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_student_information, container, false);
unbinder = ButterKnife.bind(this, view);
LoadDataAndSetupViewPager();
//setupViewPager(viewPager);
mTabLayout.setupWithViewPager(viewPager);
mTabLayout.addOnTabSelectedListener(this);
spinner.setOnItemSelectedListener(this);
// ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_dropdown_item, MyConfiguration.CLASS_SECTIONS);
// spinner.setAdapter(arrayAdapter);
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Fragment fragment,String tag) {
if (mListener != null) {
mListener.onClickedMenu(fragment,tag);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnConnectingFragments) {
mListener = (OnConnectingFragments) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
/*private void setupViewPager(ViewPager viewPager) {
StudentViewPagerAdapter adapter = new StudentViewPagerAdapter(getFragmentManager());
//adapter.addFragment(new StudentList(),"exam");
for (String claz: MyConfiguration.CLASS)
adapter.addFragment(new StudentList(), claz);
viewPager.setAdapter(adapter);
}*/
@OnClick(R.id.add)
public void OnClicked(LinearLayout view){
onButtonPressed(new AddStudent(),"addStudent");
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
className.setText(String.format(getString(R.string.class_name), tab.getPosition()+1));
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
public void LoadDataAndSetupViewPager() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
ClearableCookieJar cookieJar =
new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getActivity()));
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
Requester requester=new Retrofit.Builder()
.baseUrl(MyConfiguration.BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(JacksonConverterFactory.create())
.client(client)
.build().create(Requester.class);
disposable=new CompositeDisposable(requester.getClasses() ////GETTING CLASSES////
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.flatMapIterable(clazzs -> clazzs)
.flatMap(clazz -> requester.getDivision(clazz.getClassId()) ////GETTING SECTIONS////
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.flatMapIterable(sections -> sections)
.doOnNext(section -> {sectionsList.add(section);
Log.v("section_id",section.getSectionId());})
.takeLast(1)
.map(section -> clazz)
)
.doOnNext(clazz -> {clazz.setSections(sectionsList);
Log.v("List Size",sectionsList.size()+"");
sectionsList=new ArrayList<>();
})
.toList()
.subscribe(this::SetupViewPager, throwable -> Log.e("retroerror",throwable.toString())));
}
public void SetupViewPager(List<Clazz> classList){
StudentViewPagerAdapter adapter = new StudentViewPagerAdapter(getFragmentManager());
//adapter.addFragment(new StudentList(),"exam");
for (Clazz claz: classList){
adapter.addFragment(new StudentList(), claz);
}
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(3);
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
List<Section>sections=classList.get(position).getSections();
ArrayAdapter<Section> arrayAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_dropdown_item, sections);
spinner.setAdapter(arrayAdapter);
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Section section= (Section) parent.getItemAtPosition(position);
sectionChanger.ChangeData(section);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
public static void bindSectionChangeListener(SectionChanger changer){
sectionChanger=changer;
}
}
问题是:为什么有时在微调器中选择选项时数据会保持不变?(看看我的案例)
最佳答案
尝试使用 getChildFragmentManager() 而不是 getFragmentManager() 将解决您的问题
关于android - RecyclerView 项目没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42921034/
我正在使用 NetBeans 开发 Java 中的 WebService,并使用 gradle 作为依赖管理。 我找到了this article关于使用 gradle 开发 Web 项目。它使用 Gr
我正在将旧项目从 ant 迁移到 gradle(以使用其依赖项管理和构建功能),并且在生成 时遇到问题>eclipse 项目。今天的大问题是因为该项目有一些子项目被拆分成 war 和 jar 包部署到
我已经为这个错误苦苦挣扎了很长时间。如果有帮助的话,我会提供一些问题的快照。请指导我该怎么办????在我看来,它看起来一团糟。 *** glibc detected *** /home/shivam/
我在 Ubuntu 12.10 上运行 NetBeans 7.3。我正在学习 Java Web 开发类(class),因此我有一个名为 jsage8 的项目,其中包含我为该类(class)所做的工作。
我想知道 Codeplex、GitHub 等中是否有任何突出的项目是 C# 和 ASP.NET,甚至只是 C# API 与功能测试 (NUnit) 和模拟(RhinoMocks、NMock 等)。 重
我创建了一个 Maven 项目,包装类型为“jar”,名为“Y”我已经完成了“Maven 安装”,并且可以在我的本地存储库中找到它.. 然后,我创建了另一个项目,包装类型为“war”,称为“X”。在这
我一直在关注the instructions用于将 facebook SDK 集成到我的应用程序中。除了“helloFacebookSample”之外,我已经成功地编译并运行了所有给定的示例应用程序。
我想知道,为什么我们(Java 社区)需要 Apache Harmony 项目,而已经有了 OpenJDK 项目。两者不是都是在开源许可下发布的吗? 最佳答案 事实恰恰相反。 Harmony 的成立是
我正在尝试使用 Jsoup HTML Parser 从网站获取缩略图 URL我需要提取所有以 60x60.jpg(或 png)结尾的 URL(所有缩略图 URL 都以此 URL 结尾) 问题是我让它在
我无法构建 gradle 项目,即使我编辑 gradle 属性,我也会收到以下错误: Error:(22, 1) A problem occurred evaluating root project
我有这个代码: var NToDel:NSArray = [] var addInNToDelArray = "Test1 \ Test2" 如何在 NToDel:NSArray 中添加 addInN
如何在单击显示更多(按钮)后将主题列表限制为 5 个(项目)。 还有 3(项目),依此类推到列表末尾,然后它会显示显示更少(按钮)。 例如:在 Udemy 过滤器选项中,当您点击查看更多按钮时,它仅显
如何将现有的 Flutter 项目导入为 gradle 项目? “导入项目”向导要求 Gradle 主路径。 我有 gradle,安装在我的系统中。但是这里需要设置什么(哪条路径)。 这是我正在尝试的
我有一个关于 Bitbucket 的项目。只有源被提交。为了将项目检索到新机器上,我在 IntelliJ 中使用了 Version Control > Checkout from Ve
所以,我想更改我公司的一个项目,以使用一些与 IDE 无关的设置。我在使用 Tomcat 设置 Java 应用程序方面有非常少的经验(我几乎不记得它是如何工作的)。 因此,为了帮助制作独立于 IDE
我有 2 个独立的项目,一个在 Cocos2dx v3.6 中,一个在 Swift 中。我想从 Swift 项目开始游戏。我该怎么做? 我已经将整个 cocos2dx 项目复制到我的 Swift 项目
Cordova 绝对是新手。这些是我完成的步骤: checkout 现有项目 运行cordova build ios 以上生成此构建错误: (node:10242) UnhandledPromiseR
我正在使用 JQuery 隐藏/显示 li。我的要求是,当我点击任何 li 时,它应该显示但隐藏所有其他 li 项目。当我将鼠标悬停在文本上时 'show all list item but don
我想将我所有的java 项目(223 个项目)迁移到gradle 项目。我正在使用由 SpringSource STS 团队开发的 Gradle Eclipse 插件。 目前,我所有的 java 项目
我下载this Eclipse Luna ,对于 Java EE 开发人员,如描述中所见,它支持 Web 应用程序。我找不到 file -> new -> other -> web projects
我是一名优秀的程序员,十分优秀!