我正在尝试在片段中添加水平recylerview
,但没有显示出来。如果我将layoutManager
的方向更改为垂直,则可以使用。即使是flexLayout
也是可见的,但是水平recylerview
的效果不好。
MainActivity
public class MainActivity extends AppCompatActivity {
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.viewPager)
ViewPager viewPager;
@BindView(R.id.dots_indicator)
DotsIndicator dotsIndicator;
PagerAdapter adapter;
int fragNo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initStatusBar();
viewPager.getChildCount();
viewPager.setOffscreenPageLimit(4);
adapter = new PagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
dotsIndicator.setViewPager(viewPager);
Common.initCache();
}
private class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragOne();
case 1:
return new FragTwo();
case 2:
return new FragThree();
default:
return new FragOne();
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
fragNo = position;
return "page" + position;
}
}
private void initStatusBar() {
Window w = getWindow();
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
w.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setTintColor(ContextCompat.getColor(this, R.color.ib_fr_white));
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1) {
w.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
w.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
View view = w.getDecorView();
int flags = view.getSystemUiVisibility();
flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
view.setSystemUiVisibility(flags);
getWindow().setStatusBarColor(Color.WHITE);
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N) {
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
} else {
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
}
}
FragOne
public class FragOne extends Fragment {
@BindView(R.id.skills)
RecyclerView rvSKills;
@BindView(R.id.addedSkills)
RecyclerView rvAddedSKills;
@BindView(R.id.add_row_button)
TextView addSkill;
@BindView(R.id.editSkill)
EditText editSkill;
SuggestedSkillAdapter adapterSs;
AddedSkillsAdapter adapterAs;
Context context;
List<String> addedSkills = new ArrayList<>();
List<String> suggestedSkills = new ArrayList<>();
LinearLayoutManager layoutManagerConnection;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_frag_one, container, false);
ButterKnife.bind(this, view);
context = getContext();
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context);
rvSKills.addItemDecoration(new SpacesItemDecorationNew(0));//For equal distribution of columns.
rvSKills.setLayoutManager(layoutManager);
rvSKills.setItemAnimator(new DefaultItemAnimator());
rvSKills.setHasFixedSize(false);
String featuresAll = Constants.skills;
String[] featuresSeperated = featuresAll.split(",");
suggestedSkills.clear();
suggestedSkills.addAll(Arrays.asList(featuresSeperated));
adapterSs = new SuggestedSkillAdapter(R.layout.skill_filled_layout, suggestedSkills, context);
rvSKills.setAdapter(adapterSs);
adapterSs.notifyDataSetChanged();
layoutManagerConnection = new LinearLayoutManager(this.getActivity(),LinearLayoutManager.HORIZONTAL,false);
initAddedSkillsRv();
handleClicks();
initeditText();
return view;
}
private void initeditText() {
editSkill.requestFocus();
editSkill.setOnTouchListener((v, event) -> {
editSkill.setHint("");
return false;
});
editSkill.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
editSkill.setHint("Type a skill or select from above");
editSkill.setFilters(new InputFilter[]{filter});
editSkill.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
editSkill.setSelection(charSequence.length());
if (charSequence.length() > 3) {
addSkill.setTextColor(getResources().getColor(R.color.quantum_googgreen500));
} else {
addSkill.setTextColor(getResources().getColor(R.color.instabug_grey_hint_dark));
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
private void handleClicks() {
addSkill.setOnClickListener(view -> {
if (editSkill.getText().length() > 3 && !addedSkills.contains(editSkill.getText().toString())) {
addedSkills.add(0, editSkill.getText().toString());
} else Toast.makeText(context, "Add Something", Toast.LENGTH_SHORT).show();
});
}
public void initAddedSkillsRv() {
rvAddedSKills.setNestedScrollingEnabled(true);
rvAddedSKills.setLayoutManager(layoutManagerConnection);
adapterAs = new AddedSkillsAdapter(R.layout.skill_filled_layout, suggestedSkills, context);
rvAddedSKills.setAdapter(adapterAs);
adapterSs.notifyDataSetChanged();
}
InputFilter filter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
boolean keepOriginal = true;
StringBuilder sb = new StringBuilder(end - start);
for (int i = start; i < end; i++) {
char c = source.charAt(i);
if (isCharAllowed(c)) // put your condition here
sb.append(c);
else
keepOriginal = false;
}
if (keepOriginal)
return null;
else {
if (source instanceof Spanned) {
SpannableString sp = new SpannableString(sb);
TextUtils.copySpansFrom((Spanned) source, start, sb.length(), null, sp, 0);
return sp;
} else {
return sb;
}
}
}
private boolean isCharAllowed(char c) {
return Character.isLetter(c) || Character.isSpaceChar(c);
}
};
}
不添加适配器,因为它能够查看适配器日志中的数据。
答案 0 :(得分:1)
编写此代码
rvAddedSKills .setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));