通过单击图像无法使用Android Bookmak页面

时间:2017-09-17 09:37:27

标签: android

我编写了核心,将PDF页面添加到书签的共享首选项中,但是当我单击图像时,图像既不会更改,也不会将页码添加到书签列表中。

以下是我的代码。它显示没有错误和图像被点击但页面编号没有添加到微调器中用于书签列表。

在我使用textview进行任务之前,该工作正常但现在我希望在标记或取消标记页面时更改标记图像。

   @EActivity(R.layout.activity_main)
@OptionsMenu(R.menu.actionbar)
public class PDFViewActivity extends SherlockActivity implements OnPageChangeListener, View.OnClickListener {

    public static final String SAMPLE_FILE = "myfile.pdf";

    public static final String KEY_BOOKMARKS = "bookmarks_pages";
    @ViewById
    PDFView pdfView;

    @NonConfigurationInstance
    String pdfName = SAMPLE_FILE;


    @NonConfigurationInstance
    Integer pageNumber = 1;

    SharedPreferences sharedpreferences;
    public static final String mypreference = "mypref";
    public static final String Name = "nameKey";
    public static final String Email = "emailKey";

    Spinner bookmarkSp;
    ArrayAdapter<String> dataAdapter;
    private final int TotalPages = 57;



    @AfterViews
    void afterViews() {
        sharedpreferences = getSharedPreferences(mypreference,
                Context.MODE_PRIVATE);
        display(pdfName, false);
    }


    int check = 0;

    private void display(String assetFileName, boolean jumpToFirstPage) {

        if (jumpToFirstPage) pageNumber = 1;



        int x = TotalPages;
        int[] page_seq = new int[TotalPages];
        for (int i = 0; i < TotalPages; i++) {
            page_seq[i] = --x;
            Log.d("testdesp", "" + page_seq[i]);
        }
//       .pages(2,1,0)

        pdfView.fromAsset(assetFileName)
                .defaultPage(TotalPages)
                .pages(page_seq)
                .onLoad(new OnLoadCompleteListener() {
                    @Override
                    public void loadComplete(int nbPages) {
                        ((TextView) findViewById(R.id.tv_total_page)).setText("/ " + pdfView.getPageCount());
                    }
                })
                .onPageChange(this)
                .load();

        findViewById(R.id.btn_go).setOnClickListener(this);
        findViewById(R.id.tag_btn).setOnClickListener(this);
        bookmarkSp = (Spinner) findViewById(R.id.sp_bookmark_list);

        List<String> list = new ArrayList<String>();
        String pages = sharedpreferences.getString(KEY_BOOKMARKS, "");
        String[] split = pages.split(",");
        list.add("");
        for (String val : split)
            if (val.length() > 0) {
                int value = Integer.parseInt(val);
//                value = pdfView.getPageCount() - (value - 1);
                value = TotalPages - (value - 1);
                list.add("" + value);
            }

        dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        bookmarkSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (++check > 1) {
                    String val = (String) parent.getItemAtPosition(position);
                    if (val.length() > 0) {
                        int value = Integer.parseInt(val);
                        value = pdfView.getPageCount() - (value - 1);
                        pdfView.jumpTo(value);
                    }
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        bookmarkSp.setAdapter(dataAdapter);

    }


    @Override
    public void onPageChanged(int page, int pageCount) {
        pageNumber = page;
        ((EditText) findViewById(R.id.et_page_number)).setText(pageCount - (pageNumber - 1) + "");
        if (check(page))
            ((ImageView) findViewById(R.id.tag_btn)).setImageResource(R.drawable.tagged);
        else
            ((ImageView) findViewById(R.id.tag_btn)).setImageResource(R.drawable.untaged);
    }

    @Override
    public void onBackPressed() {

        super.onBackPressed();
    }

    private boolean displaying(String fileName) {
        return fileName.equals(pdfName);
    }

    @Override
    public void onClick(View view) {

        view.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.fade_out));

        switch (view.getId()) {
            case R.id.btn_go:
                int page = Integer.parseInt(((EditText) findViewById(R.id.et_page_number)).getText().toString());
                page = TotalPages - (page - 1);
                pdfView.jumpTo(page);
                break;
            case R.id.tag_btn:
                if (((ImageView) findViewById(R.id.tag_btn)).getDrawable().getConstantState() == getResources().getDrawable(R.drawable.untaged).getConstantState() ) {
                    sharedpreferences.edit().putString(KEY_BOOKMARKS, sharedpreferences.getString(KEY_BOOKMARKS, "") + pageNumber + ",").commit();
                    ((ImageView) findViewById(R.id.tag_btn)).setImageResource(R.drawable.tagged);



                    dataAdapter.add(pdfView.getPageCount() - (pageNumber - 1) + "");
                    dataAdapter.notifyDataSetChanged();
                } else if (((ImageView) findViewById(R.id.tag_btn)).getDrawable().getConstantState() == getResources().getDrawable(R.drawable.tagged).getConstantState() ) {
                    sharedpreferences.edit().putString(KEY_BOOKMARKS, sharedpreferences.getString(KEY_BOOKMARKS, "").replace(pageNumber + ",", "")).commit();
                    ((ImageView) findViewById(R.id.tag_btn)).setImageResource(R.drawable.untaged);


                    dataAdapter.remove(TotalPages - (pageNumber - 1) + "");
                    dataAdapter.notifyDataSetChanged();
                }
                break;
        }

    }

    boolean check(int page) {
        String number = sharedpreferences.getString(KEY_BOOKMARKS, "");
        Log.d("testdisp", pdfView.getPageCount() + "  ** " + number + "   ****" + number.contains(page + ","));
        return number.contains(page + ",");
    }
}

1 个答案:

答案 0 :(得分:0)

我通过添加可绘制资源解决了我的问题

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true" android:drawable="@drawable/tagged" />
        <item android:state_checked="false" android:drawable="@drawable/untaged" />
    </selector>

并在我的java活动代码中进行了以下更改。

 if (((CheckBox) findViewById(R.id.chk_tag)).isChecked()) {
                    sharedpreferences.edit().putString(KEY_BOOKMARKS, sharedpreferences.getString(KEY_BOOKMARKS, "") + pageNumber + ",").commit();
                    ((CheckBox) findViewById(R.id.chk_tag)).setChecked(true);


                    dataAdapter.add(pdfView.getPageCount() - (pageNumber - 1) + "");
                    dataAdapter.notifyDataSetChanged();
                } else  {
                    sharedpreferences.edit().putString(KEY_BOOKMARKS, sharedpreferences.getString(KEY_BOOKMARKS, "").replace(pageNumber + ",", "")).commit();
                    ((CheckBox) findViewById(R.id.chk_tag)).setChecked(false);
}