Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android对话框中高效显示大文本视图_Android_Textview_Android Dialogfragment - Fatal编程技术网

在Android对话框中高效显示大文本视图

在Android对话框中高效显示大文本视图,android,textview,android-dialogfragment,Android,Textview,Android Dialogfragment,我试图使用一个对话框片段显示我的Android应用程序的一些许可信息,该对话框片段包含一个包装在ScrollView中的TextView。具体来说,我使用的是从GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo()方法生成的字符串。这里生成的字符串包含大量文本,当我将所需信息传递给要显示的对话框时,UI线程会冻结大约3-4秒,然后最终显示对话框。有没有一种更有效的方法可以在DialogFragment中显示大量文本 下面是实例化和显示对

我试图使用一个对话框片段显示我的Android应用程序的一些许可信息,该对话框片段包含一个包装在ScrollView中的TextView。具体来说,我使用的是从GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo()方法生成的字符串。这里生成的字符串包含大量文本,当我将所需信息传递给要显示的对话框时,UI线程会冻结大约3-4秒,然后最终显示对话框。有没有一种更有效的方法可以在DialogFragment中显示大量文本

下面是实例化和显示对话框的代码

if(googlePlayServicesLicense != null) {

   // Google Play Service License Info
    TitleParagraphViewModel googlePlayInfo = 
    new TitleParagraphViewModel("Google Play Services", 
    googlePlayServicesLicense);

    infoList.add(googlePlayInfo);
}

// Create license dialog and show
TitleParagraphListDialog dialog = 
TitleParagraphListDialog.newInstance("Licenses", infoList);

// Show the dialog
dialog.show(getSupportFragmentManager(), "dialog");
这是我的DialogFragment的代码

public class TitleParagraphListDialog extends DialogFragment {
private static final String DIALOG_TITLE_EXTRA_KEY = "dialog_title";
private static final String DIALOG_INFO_LIST_EXTRA_KEY = "info_list";
private String mDialogTitle;
private ArrayList<TitleParagraphViewModel> mInfoList;
private TextView mInformation;

public static TitleParagraphListDialog newInstance(String title, ArrayList<TitleParagraphViewModel> infoList) {
    TitleParagraphListDialog dialog = new TitleParagraphListDialog();
    Bundle args = new Bundle();
    args.putString(DIALOG_TITLE_EXTRA_KEY, title);
    args.putParcelableArrayList(DIALOG_INFO_LIST_EXTRA_KEY, infoList);
    dialog.setArguments(args);
    return dialog;
}

@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle args = getArguments();
    mDialogTitle = args.getString(DIALOG_TITLE_EXTRA_KEY) != null ? args.getString(DIALOG_TITLE_EXTRA_KEY) : "Information";
    mInfoList = (ArrayList<TitleParagraphViewModel>) (args.getParcelableArrayList(DIALOG_INFO_LIST_EXTRA_KEY) != null ? args.getParcelableArrayList(DIALOG_INFO_LIST_EXTRA_KEY) : new ArrayList<TitleParagraphViewModel>());
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    View dialogView = inflater.inflate(R.layout.title_paragraph_dialog, null);
    TextView dialogTitle = (TextView) dialogView.findViewById(R.id.dialog_title);
    mInformation = (TextView) dialogView.findViewById(R.id.information);

    dialogTitle.setText(mDialogTitle);

    dialogTitle.setTypeface(Typeface.createFromAsset(getActivity()
            .getAssets(), "fonts/Roboto-Light.ttf"));

    mInformation.setTypeface(Typeface.createFromAsset(getActivity()
            .getAssets(), "fonts/Roboto-Light.ttf"));

    for(TitleParagraphViewModel info : mInfoList) {
        mInformation.append(Html.fromHtml("<b>" + info.getTitle() + "</b>")
                + "\n\n" + info.getParagraph() + "\n\n");
    }

    builder.setView(dialogView)
    .setPositiveButton("Done", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            TitleParagraphListDialog.this.getDialog().cancel();
        }
    });
    return builder.create();
}

您可以做的是初始化某种进度条或“请稍候”对话框,然后在启动asynctask线程后立即在doInBackground上检索文本

这样你就避免了在UI线程上进行长时间调用,你的应用程序也不会冻结。 然后使用asynctask的onPostExecute删除“请稍候”对话框(或您选择使用的任何对话框)并显示文本

另一个想法是只调用滚动条中的几行,并在用户向下滚动时加载更多内容


很抱歉,如果a只回答了一些想法,希望它能给你一些启发。

我最终将长文本拆分为一个单独的行数组,然后使用s显示该行数组

代码如下:

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.android.gms.common.GoogleApiAvailability;

public class GoogleLicenseDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Activity activity = getActivity();
        AlertDialog.Builder builder = new AlertDialog.Builder(activity)
                .setPositiveButton(R.string.button_close, null);

        GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
        String licenseInfo = googleApiAvailability.getOpenSourceSoftwareLicenseInfo(activity);

        if (licenseInfo != null) {
            Context context = getDialogContext(activity);
            RecyclerView recyclerView = createRecyclerView(context);
            recyclerView.setAdapter(new LongMessageAdapter(context, licenseInfo));
            builder.setView(recyclerView);
        }

        return builder.create();
    }

    private Context getDialogContext(Context context) {
        TypedValue outValue = new TypedValue();
        int resId = android.support.v7.appcompat.R.attr.dialogTheme;
        context.getTheme().resolveAttribute(resId, outValue, true);
        int themeId = outValue.resourceId;
        return themeId == 0 ? context : new ContextThemeWrapper(context, themeId);
    }

    private RecyclerView createRecyclerView(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        @SuppressLint("InflateParams")
        RecyclerView recyclerView = (RecyclerView) inflater.inflate(R.layout.recycler_dialog, null);

        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));

        return recyclerView;
    }

    private static final class LongMessageAdapter
            extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private final Context context;
        private final String[] lines;

        public LongMessageAdapter(Context context, String message) {
            this.context = context;
            this.lines = message.split("\\n");
        }

        @Override
        public int getItemCount() {
            return lines.length;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            TextView textView = new TextView(context);
            textView.setTextAppearance(context, R.style.TextAppearance_AppCompat_Subhead);
            return new RecyclerView.ViewHolder(textView) {
            };
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ((TextView) holder.itemView).setText(lines[position]);
        }
    }
}
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.app.Dialog;
导入android.app.DialogFragment;
导入android.content.Context;
导入android.os.Bundle;
导入android.support.v7.app.AlertDialog;
导入android.support.v7.widget.LinearLayoutManager;
导入android.support.v7.widget.RecyclerView;
导入android.util.TypedValue;
导入android.view.contexthemewrapper;
导入android.view.LayoutInflater;
导入android.view.ViewGroup;
导入android.widget.TextView;
导入com.google.android.gms.common.GoogleAppAvailability;
公共类GoogleLicenseDialogFragment扩展了DialogFragment{
@凌驾
创建对话框上的公共对话框(Bundle savedInstanceState){
Activity=getActivity();
AlertDialog.Builder=新建AlertDialog.Builder(活动)
.setPositiveButton(R.string.button_close,null);
GoogleAppAvailability GoogleAppAvailability=GoogleAppAvailability.getInstance();
String licenseInfo=GoogleAppAvailability.getOpenSourceSoftwareLicenseInfo(活动);
如果(licenseInfo!=null){
上下文上下文=getDialogContext(活动);
RecyclerView RecyclerView=createRecyclerView(上下文);
setAdapter(新的LongMessageAdapter(上下文,licenseInfo));
builder.setView(recyclerView);
}
返回builder.create();
}
私有上下文getDialogContext(上下文上下文){
TypedValue outValue=新的TypedValue();
int resId=android.support.v7.appcompat.R.attr.dialogTheme;
context.getTheme().resolveAttribute(resId,outValue,true);
int-themeId=outValue.resourceId;
返回themeId==0?上下文:新的ContextThemeWrapper(上下文,themeId);
}
private RecyclerView createRecyclerView(上下文){
LayoutFlater充气机=LayoutFlater.from(上下文);
@SuppressLint(“充气参数”)
RecyclerView RecyclerView=(RecyclerView)充气器。充气(R.layout.recycler\u对话框,空);
recyclerView.setHasFixedSize(true);
setLayoutManager(新的LinearLayoutManager(上下文));
返回回收视图;
}
私有静态最终类LongMessageAdapter
扩展RecyclerView.Adapter{
私人最终语境;
私有最终字符串[]行;
公共LongMessageAdapter(上下文、字符串消息){
this.context=上下文;
this.lines=message.split(\\n”);
}
@凌驾
public int getItemCount(){
返回线长度;
}
@凌驾
public RecyclerView.ViewHolder onCreateViewHolder(视图组父级,int-viewType){
TextView TextView=新的TextView(上下文);
textView.setTextAppearance(上下文,R.style.TextAppearance\u AppCompat\u子标题);
返回新的RecyclerView.ViewHolder(textView){
};
}
@凌驾
BindViewHolder上的公共无效(RecyclerView.ViewHolder,int位置){
((TextView)holder.itemView).setText(行[位置]);
}
}
}
recycler_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="?attr/dialogPreferredPadding"
    android:paddingRight="?attr/dialogPreferredPadding"
    android:paddingTop="@dimen/abc_dialog_padding_top_material"
    android:scrollbars="vertical"/>

现在已不推荐使用。应用程序不再需要显示此信息。
<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="?attr/dialogPreferredPadding"
    android:paddingRight="?attr/dialogPreferredPadding"
    android:paddingTop="@dimen/abc_dialog_padding_top_material"
    android:scrollbars="vertical"/>