如何使Textview可滚动,并使其可以在Android

时间:2016-08-30 13:38:28

标签: android scroll textview swipe

image

我想让TextView可以滑动,也可以像上面的图片一样滚动。

下面是我的代码,TextView是适配器和活动中的滑动功能。

  public class MsgSwipActivity extends Base {

private static final String TAG = "MainActivity";
private SwipeDeck swipe_deck;
private ImageView imgThank;
private Toolbar toolbar;
private AdView mAdView;
private int page_no = 1;
private SwipMsgAdapter adapter;
private ArrayList<MsgListData> msgListDatas;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.msgswip);

    swipe_deck = (SwipeDeck) findViewById(R.id.swipe_deck);
    mAdView = (AdView) findViewById(R.id.adView);
    imgThank = (ImageView) findViewById(R.id.imgThank);
    imgThank.setVisibility(View.GONE);

    MobileAds.initialize(MsgSwipActivity.this, getString(R.string.banner_home_footer));
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();
    mAdView.loadAd(adRequest);
    // Start loading the ad in the background.
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    swipe_deck.setHardwareAccelerationEnabled(true);
    msgList();

    msgListDatas = new ArrayList<>();
    adapter = new SwipMsgAdapter(MsgSwipActivity.this, R.layout.rowmsg_item, msgListDatas);
    swipe_deck.setAdapter(adapter);
    swipe_deck.setEventCallback(new SwipeDeck.SwipeEventCallback() {
        @Override
        public void cardSwipedLeft(int position) {
            Log.i("MainActivity", "card was swiped left, position in adapter: " + position);
        }

        @Override
        public void cardSwipedRight(int position) {
            Log.i("MainActivity", "card was swiped right, position in adapter: " + position);
        }

        @Override
        public void cardsDepleted() {
            page_no = page_no + 1;
            msgList();
            msgListDatas = new ArrayList<>();
            adapter = new SwipMsgAdapter(MsgSwipActivity.this, R.layout.rowmsg_item, msgListDatas);
            swipe_deck.setAdapter(adapter);
            Log.i("MainActivity", "no more cards");
        }

        @Override
        public void cardActionDown() {
            Log.i(TAG, "cardActionDown");
        }

        @Override
        public void cardActionUp() {
            Log.i(TAG, "cardActionUp");
        }

    });
}


}

public void msgList() {
    String id = getIntent().getStringExtra("ID");
    String cat_id = getIntent().getStringExtra("CATID");
    VLogger.infoLog(cat_id);
    if (Utility.checkInternetConnection(this)) {
        RetrofitClient.getInstance().getRestOkClient().
                msgSticky("ListingShayariSingle",
                        cat_id,
                        ((VApp) getApplicationContext()).getCheck()
                        , id, String.valueOf(page_no), callback);
    } else {
        Toast.makeText(MsgSwipActivity.this, "No Intetnet please try again", Toast.LENGTH_SHORT).show();
    }
}
private final retrofit.Callback callback = new retrofit.Callback() {
    @Override
    public void success(Object object, Response response) {
        MsgList msgList = (MsgList) object;
        if (msgList.getData() != null) {
            for (int i = 0; i < msgList.getData().size(); i++) {
                MsgListData item = new MsgListData();
                item.setId(msgList.getData().get(i).getId());
                item.setReg_date(msgList.getData().get(i).getReg_date());
                item.setStatus(msgList.getData().get(i).getStatus());
                item.setDetails(msgList.getData().get(i).getDetails());
                item.setCategory(msgList.getData().get(i).getCategory());
                item.setLanguage(msgList.getData().get(i).getLanguage());
                item.setColor_code(msgList.getData().get(i).getColor_code());
                getSupportActionBar().setTitle(msgList.getData().get(i).getC_name().toString());

                msgListDatas.add(item);
            }
            adapter.setGridData(msgListDatas);
        } else {
            imgThank.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void failure(RetrofitError error) {
        Toast.makeText(MsgSwipActivity.this, "Server Error", Toast.LENGTH_SHORT).show();
    }
};
 }

这是我的适配器

 public class SwipMsgAdapter extends ArrayAdapter<MsgListData>{

private ArrayList<MsgListData> rowDatas;
private Context context;
private int layoutResourceId;
public EditText edtSave;
public String image_Id;
private boolean checked;

Uri bmpUri;
private ImageView imgFav, imgCopy, imgFb, imgShare, imgHike;
private ImageView imgWhatsapp;
private TextView imgMain;
private View positiveAction;


CallbackManager callbackManager;
ShareDialog shareDialog;
public SwipMsgAdapter(Context context, int layoutResourceId, ArrayList<MsgListData> rowDatas) {

    super(context, layoutResourceId, rowDatas);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.rowDatas = rowDatas;

}

public void setGridData(ArrayList<MsgListData> rowDatas) {
    this.rowDatas = rowDatas;
    notifyDataSetChanged();
}

@Override
public int getCount() {
    return rowDatas.size();
}


@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    FacebookSdk.sdkInitialize(context.getApplicationContext());

    View v = convertView;
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // normally use a viewholder
        v = inflater.inflate(R.layout.rowmsg_item, parent, false);
    }
    callbackManager = CallbackManager.Factory.create();
    shareDialog = new ShareDialog((Activity) context);
    imgMain = (TextView) v.findViewById(R.id.imgMain);
    imgFav = (ImageView) v.findViewById(R.id.imgFav);
    imgCopy = (ImageView) v.findViewById(R.id.imgCopy);
    imgHike = (ImageView) v.findViewById(R.id.imgHike);
    imgWhatsapp = (ImageView) v.findViewById(R.id.imgWhatsapp);
    imgFb = (ImageView) v.findViewById(R.id.imgFb);
    imgShare = (ImageView) v.findViewById(R.id.imgShare);

    CardView card_view = (CardView) v.findViewById(R.id.card_view);
    int color = Color.parseColor(rowDatas.get(position).getColor_code());

    card_view.setCardBackgroundColor(color);
    image_Id = rowDatas.get(position).getId();

    imgMain.setText(rowDatas.get(position).getDetails());
    VLogger.infoLog("IMAGE" + String.valueOf(bmpUri));
    RetrofitClient.getInstance().getRestOkClient().
            checkMsgFavorite("AvailableFavoritText", VPreferences.getPreferanceDeviceID(context)
                    , rowDatas.get(position).getId(), checkCallback);


};

如何实现功能滚动和滑动?

2 个答案:

答案 0 :(得分:1)

Scrollable TextView:将TextView作为ScrollView的子项。

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />
</ScrollView>

请记住,ScrollView只能容纳一个对象。

对于手势,请查看Detecting Common Gestures

编辑:

您也可以在XML中使用这行代码,看看它是否有效(使用textview):

android:scrollbars="vertical"

答案 1 :(得分:0)

将textview放在xml中的scrollview中。然后以编程方式“刷卡”。 Stackoverflow告诉我,我的答案不符合他们的质量标准。