Public class MainActivity {
static MainActivity main;
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new Maandag_home(), "MA");
viewPagerAdapter.addFragments(new Dinsdag_home(), "DI");
viewPagerAdapter.addFragments(new Woensdag_home(), "WO");
viewPagerAdapter.addFragments(new Donderdag_home(), "DO");
viewPagerAdapter.addFragments(new Vrijdag_home(), "VR");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
Viewpager适配器代码:
package com.mycompany.myapp;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> fragments = new ArrayList<>();
ArrayList<String> tabTitles = new ArrayList<>();
public void addFragments(Fragment fragments, String titles)
{
this.fragments.add(fragments);
this.tabTitles.add(titles);
}
public ViewPagerAdapter (FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles.get(position);
}
}
package com.mycompany.myapp;
import android.graphics.Color;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class MyRecyclerViewAdapter extends RecyclerView
.Adapter<MyRecyclerViewAdapter
.DataObjectHolder> {
private static String LOG_TAG = "MyRecyclerViewAdapter";
private ArrayList<DataObject> mDataset;
private static MyClickListener myClickListener;
public static class DataObjectHolder extends RecyclerView.ViewHolder
implements View
.OnClickListener {
TextView label;
TextView dateTime;
TextView tijd;
CardView cardView;
public DataObjectHolder(View itemView) {
super(itemView);
label = (TextView) itemView.findViewById(R.id.textView);
dateTime = (TextView) itemView.findViewById(R.id.textView2);
tijd = (TextView) itemView.findViewById(R.id.textView3);
cardView = (CardView) itemView.findViewById(R.id.card_view);
Log.i(LOG_TAG, "Adding Listener");
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
myClickListener.onItemClick(getAdapterPosition(), v);
}
}
public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}
public MyRecyclerViewAdapter(ArrayList<DataObject> myDataset) {
mDataset = myDataset;
}
@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view_row, parent, false);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}
@Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
holder.label.setText(mDataset.get(position).getmText1());
holder.dateTime.setText(mDataset.get(position).getmText2());
holder.tijd.setText(mDataset.get(position).getmText3());
String color = mDataset.get(position).getmState();
if (color == "FFFFFF") {}
else if (color == "F44336") {
holder.cardView.setCardBackgroundColor(0xFFF44336);
holder.label.setTextColor(0xFFFFFFFF);
holder.dateTime.setTextColor(0xFFFFFFFF);
}
else if (color == "4CAF50"){
holder.cardView.setCardBackgroundColor(0xFF4CAF50);
holder.label.setTextColor(0xFFFFFFFF);
holder.dateTime.setTextColor(0xFFFFFFFF);
}
else if (color == "FFEB3B") {
holder.cardView.setCardBackgroundColor(0xFFFFEB3B);
}
else if (color == "607D8B") {
holder.cardView.setCardBackgroundColor(0xFF607D8B);
}
}
public void addItem(DataObject dataObj, int index) {
mDataset.add(index, dataObj);
notifyItemInserted(index);
}
public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}
@Override
public int getItemCount() {
return mDataset.size();
}
public interface MyClickListener {
public void onItemClick(int position, View v);
}
}
DataObject obj = new DataObject("My Subject", "My Teacher", "Time", "FFFFFF");
((MyRecyclerViewAdapter) mAdapter).addItem(obj, index);
1/更改以下内容:
public MyRecyclerViewAdapter(ArrayList<DataObject> myDataset) {
mDataset = myDataset;
}
对此:
//使用getActivity()从片段中获取活动并传递给构造函数
public MyRecyclerViewAdapter(Activity activity, ArrayList<DataObject> myDataset) {
mDataset = myDataset;
mActivity = activity;
}
public void addItem(DataObject dataObj, int index) {
mDataset.add(index, dataObj);
notifyItemInserted(index);
}
public void addItem(DataObject dataObj, final int index) {
if (mActivity == null){
return;
}
mActivity.runOnUiThread(new Runnable() {
@Override public void run() {
mDataset.add(index, dataObj);
notifyItemInserted(index);
}
});
}
我正在尝试将RecyclerView居中,但其layout_width为wrap_content时未成功 当RecyclerView被给定任何确定的layout_width(比如200dp)时,它会居中,否则它只会向左对齐。 如何使回收器视图center_horizontal,当其layout_width wrap_content?
我见过的变通方法,但没有。有什么想法吗?
Java运行时环境检测到一个致命错误: SIGSEGV(0xB)在PC=0x00007FBFFF58A061,PID=4562,TID=140465537804032 JRE版本:Java(TM)SE运行时环境(7.0_51-B13)(build 1.7.0_51-B13)Java VM:Java HotSpot(TM)64位服务器VM(24.51-B03混合模式linux-amd64压缩oops
我有一个recycle view,用户可以在其中将一个项目拖放到recycle view中的不同位置。 到目前为止,一切正常。 我的问题始于RecyclerView中的项目超过了它可以显示的数量。所以当它回收他的内容时。当用户将项目拖放到不同的位置,然后滚动离开该零件时,不会保存位置更改。用户只看到项目的旧位置。 您可以在下面的.gif中看到此问题。 我已经尝试了几件事,比如: 和 在我的onBi
我正在尝试使回收器视图中的cardview展开。我让扩展部分正常工作,但是当向其添加过渡时,开始出现一些视觉错误。当没有屏幕外项目时,过渡工作正常,但是当我向回收器视图添加超过(在我的情况下)4个项目时,它开始发生。 带4件物品的GIF 包含 4 个以上项目的 GIF 当我禁用过渡动画时,卡视图扩展可以很好地处理超过4个项目。我认为问题与职位变化有关,但我找不到解决问题的任何解决方案。 我用来实现
我的布局结构如下: 当我将RecyclerView添加到布局时,NestedScrollView将位于AppBarLayout下,如下图所示: 我测试了一些解决这个问题的方法,比如将NestedScrollView放入FrameLayout,或者向NestedScrollView添加和属性,但这些方法无法解决这个问题。