- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个TestActivity
:
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
// Setup the Tabs ViewPager
final SectionsPagerAdapter pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
ViewPager pager = (ViewPager) findViewById(R.id.pager);
// Set the number of tabs to keep in memory
pager.setOffscreenPageLimit(3);
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
int currentPosition = 0;
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int newPosition) {
FragmentLifecycle fragmentToShow = (FragmentLifecycle)pagerAdapter.getItem(newPosition);
fragmentToShow.onResumeFragment();
FragmentLifecycle fragmentToHide = (FragmentLifecycle)pagerAdapter.getItem(currentPosition);
fragmentToHide.onPauseFragment();
currentPosition = newPosition;
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
pager.setAdapter(pagerAdapter);
// Attach the ViewPager to the TabLayout
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(pager);
}
private class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new TestFragment();
case 1:
return new TestFragment();
case 2:
return new TestFragment();
case 3:
return new TestFragment();
}
return null;
}
@Override
public int getCount() {
return 4;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Test1";
case 1:
return "Test2";
case 2:
return "Test3";
case 3:
return "Test4";
}
return null;
}
}
}
通过 ViewPager
创建 4 个 TestFragment
:
public class TestFragment extends Fragment implements FragmentLifecycle {
private RecyclerView testRecycler;
private ArrayList<TestItem> items;
public TestFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_test, container, false);
/////////////////// CREATE TEST LIST //
items = new ArrayList<TestItem>();
items.add(new TestItem(0, "First1", "Last1"));
items.add(new TestItem(1, "First2", "Last2"));
items.add(new TestItem(2, "First3", "Last3"));
items.add(new TestItem(3, "First4", "Last4"));
items.add(new TestItem(4, "First5", "Last5"));
items.add(new TestItem(5, "First6", "Last6"));
items.add(new TestItem(6, "First7", "Last7"));
items.add(new TestItem(7, "First8", "Last8"));
items.add(new TestItem(8, "First9", "Last9"));
items.add(new TestItem(9, "First10", "Last10"));
items.add(new TestItem(10, "First11", "Last11"));
items.add(new TestItem(11, "First12", "Last12"));
items.add(new TestItem(12, "First13", "Last13"));
items.add(new TestItem(13, "First14", "Last14"));
items.add(new TestItem(14, "First15", "Last15"));
items.add(new TestItem(15, "First16", "Last16"));
items.add(new TestItem(16, "First17", "Last17"));
items.add(new TestItem(17, "First18", "Last18"));
items.add(new TestItem(18, "First19", "Last19"));
items.add(new TestItem(19, "First20", "Last20"));
items.add(new TestItem(20, "First21", "Last21"));
items.add(new TestItem(21, "First22", "Last22"));
items.add(new TestItem(22, "First23", "Last23"));
items.add(new TestItem(23, "First24", "Last24"));
items.add(new TestItem(24, "First25", "Last25"));
items.add(new TestItem(25, "First26", "Last26"));
items.add(new TestItem(26, "First27", "Last27"));
items.add(new TestItem(27, "First28", "Last28"));
items.add(new TestItem(28, "First29", "Last29"));
items.add(new TestItem(29, "First30", "Last30"));
/////////////////// SETUP THE TEST LIST //
testRecycler = (RecyclerView) layout.findViewById(R.id.test_recycler);
TestAdapter testAdapter = new TestAdapter(getContext(),
items);
testRecycler.setAdapter(testAdapter);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 1);
testRecycler.setLayoutManager(gridLayoutManager);
return layout;
}
@Override
public void onPauseFragment() {
}
@Override
public void onResumeFragment() {
}
}
一切正常,自定义适配器 TestAdapter
,排列一切:
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {
private Context context;
private Listener listener;
private ArrayList<TestItem> items;
interface Listener {
void onClick(int position, int id);
}
public TestAdapter(Context context, ArrayList<TestItem> items){
super();
this.context = context;
this.items = items;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private CardView cardView;
private TextView pupilFullNameTextView;
private CheckBox absentCheckbox;
private RadioButton failingButton;
private RadioButton strugglingButton;
private RadioButton meetingButton;
private RadioButton exceedingButton;
public ViewHolder(CardView view) {
super(view);
cardView = view;
pupilFullNameTextView = view.findViewById(R.id.fullName);
absentCheckbox = view.findViewById(R.id.absentCheckbox);
failingButton = view.findViewById(R.id.failing);
strugglingButton = view.findViewById(R.id.struggling);
meetingButton = view.findViewById(R.id.meeting);
exceedingButton = view.findViewById(R.id.exceeding);
}
}
public void setListener(Listener listener) {
this.listener = listener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
CardView cv = (CardView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_test, parent, false);
return new ViewHolder(cv);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
CardView cardView = holder.cardView;
// Set up the pupils name
holder.pupilFullNameTextView.setText(items.get(position).getFullName());
}
@Override
public int getItemCount() {
return items.size();
}
}
但是在绑定(bind)过程中,它实在是太慢了。加载所有 4 个 fragment 最多可能需要 5 秒。
我曾尝试将 setAdapter 方法移动到 AsyncTask
,这样我就可以加载 fragment 然后绑定(bind)数据,但是由于 UI 线程隔离,这无法工作。
但我忍不住想我做错了什么,因为它不应该花这么长时间!
我感觉问题可能出在 RecyclerView
中使用的 CardView
的 ViewModel 中:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="2dp"
android:theme="@style/NormalTextAppearance">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image_view"
android:layout_width="60dp"
android:layout_height="60dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@drawable/ic_default_pic"/>
<TextView
android:id="@+id/fullName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@+id/image_view"
card_view:layout_constraintBottom_toBottomOf="@+id/radioGroup"
card_view:layout_constraintTop_toTopOf="@+id/radioGroup"/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="@+id/image_view"
app:layout_constraintTop_toTopOf="@+id/image_view"
card_view:layout_constraintEnd_toEndOf="parent">
<RadioButton
android:id="@+id/failing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:buttonTint="@color/belowExpectations"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintTop_toTopOf="parent"/>
<RadioButton
android:id="@+id/struggling"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:buttonTint="@color/meetingExpectations"
card_view:layout_constraintEnd_toStartOf="@+id/belowExpectations"
card_view:layout_constraintTop_toTopOf="parent"/>
<RadioButton
android:id="@+id/meeting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:buttonTint="@color/exceedingExpectations"
card_view:layout_constraintEnd_toStartOf="@+id/meetingExpectations"
card_view:layout_constraintTop_toTopOf="parent"/>
<RadioButton
android:id="@+id/exceeding"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:buttonTint="@color/excellingExpectations"
card_view:layout_constraintEnd_toStartOf="@+id/exceedingExpectations"
card_view:layout_constraintTop_toTopOf="parent"/>
</RadioGroup>
<CheckBox
android:id="@+id/absentCheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Absent"
app:layout_constraintBottom_toBottomOf="@+id/radioGroup"
app:layout_constraintEnd_toStartOf="@+id/radioGroup"
app:layout_constraintTop_toTopOf="@+id/radioGroup"/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
是不是根深蒂固了!?
有更好的方法吗?我正在尝试将数据加载到多个 CardView
条目中,然后通过按下四个 RadioButton
之一来记录数据:
为此苦苦挣扎...如果有人可以提出一种延迟数据绑定(bind)的方法并首先加载 fragment ,那将是一个舒适的最坏情况。 FragmentDialog
表示“正在加载”。如前所述,当我尝试这样做时,我惨遭失败。
最佳答案
findViewById
在性能方面非常昂贵。你应该做的是仅在创建 ViewHolder
时使用它(在 ViewHolders 构造函数中),在 ViewHolder
中保留对 View
的引用,然后在绑定(bind) ViewHolder
时访问它,例如:
holder.textView.setText("hello")
View 持有者:
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public CheckBox checkBox;
public ViewHolder(CardView view) {
super(view);
textView = view.findViewByid(...)
...
}
}
关于android - RecyclerView onBindViewHolder 导致严重的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51598957/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!