RecyclerView滚动到底部,项目为WebView

时间:2019-05-24 07:56:16

标签: android

我有一个列表,每个项目都是WebView。我想在加载数据并调用adapter.notifyDataSetChanged之后,将RecyclerView滚动到代码myRv.setScrollToPosition(data.lenght-1)的底部,但是它不起作用,我尝试将WebView替换为TextView html,它将起作用。但我想在项目中使用WebView。 这是我的代码: MvWvActivity

class WvRvActivity extends AppCompatActivity {
    RecyclerView rv;
    private String strTable = "<div><div>\n" +
            "    \n" +
            "    <table style=\"border-width:1px; border-style:solid; width:100%\">\n" +
            "    <colgroup><col style=\"width:50%\"><col style=\"width:50%\"></colgroup>\n" +
            "    <tbody>\n" +
            "    <tr style=\"border-width:1px; border-style:solid\">\n" +
            "    <td style=\"border-width:1px; border-style:solid; padding:0.3rem 0.6rem\">okok</td>\n" +
            "    <td style=\"border-width:1px; border-style:solid; padding:0.3rem 0.6rem\">plolo</td>\n" +
            "    </tr>\n" +
            "    <tr style=\"border-width:1px; border-style:solid\">\n" +
            "    <td style=\"border-width:1px; border-style:solid; padding:0.3rem 0.6rem\">okok</td>\n" +
            "    <td style=\"border-width:1px; border-style:solid; padding:0.3rem 0.6rem\">pokok</td>\n" +
            "    </tr>\n" +
            "    </tbody>\n" +
            "    </table>\n" +
            "    \n" +
            "    \n" +
            "    <div>&nbsp;</div>\n" +
            "    </div>\n" +
            "    </div>";
    String content = "<h1>Heading 1</h1>\n" +
            "        <h2>Heading 2</h2>\n" +
            "        <p>This is some html. Look, here\\'s an <u>underline</u>.</p>\n" +
            "        <p>Look, this is <em>emphasized.</em> And here\\'s some <b>bold</b>.</p>\n" +
            "        <p>Here are UL list items:\n" +
            "        <ul>\n" +
            "        <li>One</li>\n" +
            "        <li>Two</li>\n" +
            "        <li>Three</li>\n" +
            "        </ul>\n" +
            "        <p>Here are OL list items:\n" +
            "        <ol>\n" +
            "        <li>One</li>\n" +
            "        <li>Two</li>\n" +
            "        <li>Three</li>\n" +
            "        </ol>";
    String s1 = "<h1>This is a Heading</h1>\n" +
            "<p>This is a paragraph.</p>\n" +
            "<ol>\n" +
            "    <li>1\n" +
            "    </li><li>2\n" +
            "</li><li>3\n" +
            "</li>\n" +
            "</ol>\n" +
            "<ul>\n" +
            "    <li>1\n" +
            "    </li><li>2\n" +
            "</li><li>3\n" +
            "</li>\n" +
            "</ul>\n" +
            "<blockquote>\n" +
            "    <div>Quote</div>\n" +
            "</blockquote>\n" +
            "<div><font size=\"5\" color=\"green\">Large</font></div>\n" +
            "\n" +
            "<div>medium</div>\n" +
            "\n" +
            "<div>tvhnd9x@gmail.com</div>\n" +
            "<div><span style=\"background-color:#fcd116\">hightlight</span></div>\n" +
            "\n" +
            "<div><span style=\"color:#ec8f45\">automatic</span></div>\n" +
            "</div>";
    private String s5 = "<ul>\n" +
            "  <li>Coffee</li>\n" +
            "  <li>Tea</li>\n" +
            "</ul>\n" +
            "<at id=\"f429c6ed-b865-43a2-a62f-b88c97badd1b\" onclick=\"clickMention(this.id)\">tranhoa</at>\n" +
            "<at id=\"1234\">tran bien</at>\n" +
        
            "\n" +
            "<p>Click the button to change the text of the first list item (index 0).</p>\n" +
            "\n" +
            "<button onclick=\"myFunction()\">Try it</button>";
    private String urlList[] = {s5, strTable, content, s1, s5, strTable, content, s1, s5, strTable, content, s1, s5, strTable, content, s1, s5, strTable, content, s1, s5, strTable, content, s1, s5, strTable, content, s1, s5, strTable, content, s1, s5, strTable, content, s1, s5, strTable, content, s1, s5, strTable, content, s1, s5};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wv_rv);
        ParentAdapter adapter = new ParentAdapter(urlList);
        rv = findViewById(R.id.rv);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(layoutManager);
        rv.setAdapter(adapter);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                rv.scrollToPosition(urlList.length-1);
            }
        }, 200);
    }

`ParentAdapter`
class ParentAdapter extends RecyclerView.Adapter<ParentAdapter.MyViewHolder> {
    private String[] urlList;

    public ParentAdapter(String[] urlList) {
        this.urlList = urlList;
    }

    final String EMAIL_PATTERN = Patterns.EMAIL_ADDRESS.pattern();

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row_my_web, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final @NonNull MyViewHolder holder, int position) {
        String content = urlList[position].replaceAll(EMAIL_PATTERN, "<mail onclick=\"clickEmail(event)\">$0</mail>") + JsWebView.jsWebView;
    holder.myWebView.loadData(content + JsWebView.jsWebView, "text/html", "UTF-8");
    holder.tv.setText(Html.fromHtml( urlList[position]));
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {

        MyWebView myWebView;
        TextView tv;

        MyViewHolder(View itemView) {
            super(itemView);
            myWebView = itemView.findViewById(R.id.myWebView);
            tv = itemView.findViewById(R.id.tv);
            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            myWebView.addJavascriptInterface(new WebAppInterface1(myWebView.getContext()), "Android");
        }
    }

    class WebAppInterface1 {
        Context mContext;

        WebAppInterface1(Context c) {
            mContext = c;
        }

        @JavascriptInterface
        public void clickMention(String idMention) {
            Toast.makeText(mContext, idMention, Toast.LENGTH_SHORT).show();
        }

        @JavascriptInterface
        public void clickPhone(String phone) {
            Toast.makeText(mContext, phone, Toast.LENGTH_SHORT).show();
        }

        @JavascriptInterface
        public void clickEmail(String phone) {
            Toast.makeText(mContext, phone, Toast.LENGTH_SHORT).show();
        }
    }

0 个答案:

没有答案
相关问题