我有一个DialogFragment包含RecyclerView(卡片列表).
其中RecyclerView一个或多个CardViews可以具有任何高度.
我想DialogFragment根据CardViews其中包含的内容给出正确的高度.
通常情况下,这将是简单的,我会成立wrap_content的RecyclerView这个样子.
<android.support.v7.widget.RecyclerView ...
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:scrollbars="vertical" >
</android.support.v7.widget.RecyclerView>
Run Code Online (Sandbox Code Playgroud)
因为我使用的RecyclerView这个不起作用,请看:
https://issuetracker.google.com/issues/37001674
和
在这两个页面上,人们建议扩展LinearLayoutManager和覆盖onMeasure()
我首先使用了第一个链接中提供的LayoutManager:
public static class WrappingLayoutManager extends LinearLayoutManager {
public WrappingLayoutManager(Context context) {
super(context);
}
private int[] mMeasuredDimension = new int[2];
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
int widthSpec, int heightSpec) {
final int widthMode = View.MeasureSpec.getMode(widthSpec);
final …Run Code Online (Sandbox Code Playgroud) android android-layout android-fragments android-studio android-recyclerview
我正在将Activity更改为Fragment,并在我充实RecyclerView后立即收到以下错误.
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
----> View rootView = inflater.inflate(R.layout.layout_containing_recyclerview,
container, false);
Run Code Online (Sandbox Code Playgroud)
java.lang.IllegalStateException:RecyclerView没有LayoutManager
在我将Activity变为Fragment之前,inflate就没事了.
一些进一步的研究表明,从recyclerview布局中删除所有子元素有助于解决问题.但是我不明白为什么这会改变任何事情以及为什么它之前的工作与一项活动有关.
作品
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:scrollbars="vertical" >
</android.support.v7.widget.RecyclerView>
Run Code Online (Sandbox Code Playgroud)
什么都不行
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:scrollbars="vertical" >
<View
android:id="@+id/randomview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</android.support.v7.widget.RecyclerView>
Run Code Online (Sandbox Code Playgroud)
我在这里缺少什么?
android android-fragments android-5.0-lollipop android-recyclerview
在 Jetpack Compose 中有一个Modifier名为selectable.
将组件配置为可选择的,通常作为互斥组的一部分,在任何时间点只能选择一个项目。
我将它用于可滚动列表内的互斥无线电组。在我的情况下LazyColumn。这工作正常,单击可选区域会点亮它们并导致检测到点击。但是我注意到在滚动时“触摸”这些区域时该区域也会亮起。
如果您想了解我的意思,我制作了一个可组合的简单示例,只需滚动列表,您就会看到滚动如何触发短选择状态:
@Composable
fun Example() {
LazyColumn {
item {
repeat(100){
Column(
modifier = Modifier
.fillMaxWidth()
.height(40.dp)
.selectable(
selected = false,
onClick = { }
)
) {
Text("Example")
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有人想出如何解决某种行为?我尝试在https://developer.android.com/jetpack/compose/gestures寻找任何相关文档,但没有真正解释如何在滚动时“阻止”触摸事件。
我正在启动asynctask一个SherlockListFragment在内部创建的内部SherlockFragmentActivity选项卡。
我将asynctask活动上下文传递给构造函数,并在内部初始化asynctask onCreate():
AsyncTask<String, Integer, String[]> asynctask = new DownloadFilesTask(getSherlockActivity()).execute(url);
Run Code Online (Sandbox Code Playgroud)
AsyncTask类DownloadFilesTask内部的构造函数如下所示:
private ProgressDialog dialog;
private SherlockFragmentActivity activity;
public DownloadFilesTask(SherlockFragmentActivity activity) {
this.activity = activity;
this.dialog = new ProgressDialog(activity);
}
Run Code Online (Sandbox Code Playgroud)
执行前和执行后的样子如下:
protected void onPreExecute(){
Log.d("AsyncTask!", "Showing dialog now!"); //shown in logcat
dialog.setMessage("Retrieving all currently airing anime. Please wait.");
dialog.setCancelable(false);
dialog.show();
}
Run Code Online (Sandbox Code Playgroud)
。
protected void onPostExecute(String[] result) {
Log.d("AsyncTask!", "Dismissing dialog now!"); //shown in logcat
dialog.dismiss();
}
Run Code Online (Sandbox Code Playgroud)
但是,在完成所有后台工作后,进度对话框不会显示!我在这里做错了什么?我认为这可能是一个上下文问题。
鉴于字符串"Hello World (27348)".
如何"Hello World"通过专门删除从正则表达式开始"("和结束的部分字符串来转换它")"?
string.replaceAll("???", "");
Run Code Online (Sandbox Code Playgroud) 在我的内部onCreate,Activity我做了以下几点:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AsyncTask<String, Integer, String[]> asynctask = new DownloadFilesTask(this.getActivity()).execute(url);
String[] values = null;
try {
values = asynctask.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Log.d("AsyncTask", "DONE!");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
Run Code Online (Sandbox Code Playgroud)
这有效,但我阻止了UI线程asynctask.get();,阻止我在执行后台任务期间显示任何新的UI元素,如对话框.
所以我的问题是:如何在不阻止给定此代码的UI线程的情况下从AsyncTask获取结果值?