質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

0回答

261閲覧

ListViewとViewPagerを組み合わせた処理について gravityによる不具合

tinkai

総合スコア17

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

1クリップ

投稿2018/10/06 15:12

質問内容

ListViewとViewPagerを組み合わせた画面を作成しています。
ViewPagerは2つの画面を持っており、1つ目には項目名、2つ目には削除ボタンを設定しています。
そして、そのViewPagerがListViewとして並んでいるという画面です(下画像)。
また、Listにクリック処理とロングクリック処理を追加するために、ViewPagerにClickListenerとLongClickListenerを設定しています。

画面 ListView

Page1の項目名を中央に表示させようと思い、android:gravity="center"を書き加えたところ、ListViewの切り替え(フリック処理?)が動作しなくなりました。画面の右端部分はなぜか利きました。"left","right"はしっかりと動作しました。

また、"center"を設定した状態でClickListenerらを消すときちんと動作しました。

gravity設定後

これについて質問なのですが、なぜ、gravityを"center"にしたときだけListViewの画面変更ができなくなるのでしょうか?
ClickListenerを消すと動作するので、そのあたりが怪しいと思うのですが、よく分かりません。
理由と解決方法を教えて頂きたいです。
ご回答よろしくお願いいたします。

ソースコード

ソースコードを載せますが、下記のURLを参考にテストしたため、ほとんど一緒です。
クリック処理などは一番下のMyPagerAdapter.javaファイルに追加しています。

page1.xml
問題のコード

xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 7 <TextView 8 android:id="@+id/text" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" 11 android:layout_gravity="center" 12 <!-- 13 問題の部分 動作しなくなる 14 android:gravity="center" 15 --> 16 android:singleLine="true" 17 android:textSize="38sp" /> 18 19</LinearLayout>

activity_main.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 tools:context=".MainActivity"> 9 10 <ListView 11 android:id="@+id/listView" 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" > 14 </ListView> 15 16</LinearLayout>

row.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <android.support.v4.view.ViewPager 8 android:id="@+id/viewpager" 9 android:layout_width="match_parent" 10 android:layout_height="50dp" 11 android:layout_gravity="center" /> 12 13</LinearLayout>

page2.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal"> 6 7 <RelativeLayout 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" 10 android:layout_weight="1" 11 android:background="#00000000" > 12 </RelativeLayout> 13 14 <Button 15 android:id="@+id/button2" 16 android:layout_width="70dp" 17 android:layout_height="match_parent" 18 android:background="#cc2828" 19 android:text="削除" /> 20 21</LinearLayout>

MainActivity.java

java

1public class MainActivity extends AppCompatActivity { 2 private String str[] = {"項目1","項目2","項目3","項目4","項目5","項目6","項目7","項目8","項目9","項目10"}; 3 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 9 ListView listView = findViewById(R.id.listView); 10 MyListAdapter adapter = new MyListAdapter(this, R.layout.row, str); 11 listView.setAdapter(adapter); 12 } 13}

MyListAdapter.java

java

1public class MyListAdapter extends ArrayAdapter<String> { 2 private LayoutInflater inflater = null; 3 private static final float BUTTON_WIDTH_DP = 70f; 4 private int margin; 5 6 public MyListAdapter(Context context, int resource, String[] items) { 7 super(context, resource,items); 8 inflater = LayoutInflater.from(context); 9 10 //ページ2のRelativeLayoutの幅を計算してmarginへ格納する。 11 float density = getContext().getResources().getDisplayMetrics().density; 12 int buttonWidthPX = (int) (BUTTON_WIDTH_DP * density + 0.5f); 13 14 WindowManager wm = (WindowManager)getContext().getSystemService(getContext().WINDOW_SERVICE); 15 Display dp = wm.getDefaultDisplay(); 16 17 Point realSize = new Point(); 18 dp.getRealSize(realSize); 19 20 this.margin = realSize.x - buttonWidthPX; 21 } 22 23 @Override 24 public View getView(int position, View convertView, ViewGroup parent) { 25 if(convertView == null){ 26 convertView = inflater.inflate(R.layout.row,null); 27 } 28 29 ViewPager viewPager = (ViewPager)convertView.findViewById(R.id.viewpager); 30 viewPager.setPageMargin(-margin); 31 MyPagerAdapter adapter = new MyPagerAdapter(getContext(),getItem(position)); 32 viewPager.setAdapter(adapter); 33 34 return convertView; 35 } 36}

MyPagerAdapter.java

java

1public class MyPagerAdapter extends PagerAdapter { 2 private LayoutInflater inflater; 3 4 private static final int PAGE_NUM = 2; 5 private String str; 6 7 public MyPagerAdapter(Context context, String str) { 8 super(); 9 inflater = LayoutInflater.from(context); 10 this.str = str; 11 } 12 13 @Override 14 public Object instantiateItem(ViewGroup container, int position) { 15 LinearLayout layout = null; 16 if(position == 0){ 17 layout = (LinearLayout)inflater.inflate(R.layout.page1, null); 18 TextView text = (TextView)layout.findViewById(R.id.text); 19 text.setText(str); 20 21 layout.setOnClickListener(new View.OnClickListener() { 22 @Override 23 public void onClick(View v) { 24 Log.d("log", "=========================Click"); 25 } 26 }); 27 28 layout.setOnLongClickListener(new View.OnLongClickListener() { 29 @Override 30 public boolean onLongClick(View v) { 31 Log.d("log", "==========================Long"); 32 return true; 33 } 34 }); 35 }else{ 36 layout = (LinearLayout)inflater.inflate(R.layout.page2, null); 37 38 Button button = layout.findViewById(R.id.button2); 39 button.setOnClickListener(new View.OnClickListener() { 40 @Override 41 public void onClick(View v) { 42 Log.d("log", "==========================Button"); 43 } 44 }); 45 } 46 47 container.addView(layout); 48 return layout; 49 } 50 51 @Override 52 public void destroyItem(ViewGroup container, int position, Object object) { 53 ((ViewPager) container).removeView((View) object); 54 } 55 56 @Override 57 public int getCount() { 58 return PAGE_NUM; 59 } 60 61 @Override 62 public boolean isViewFromObject(View view, Object obj) { 63 return view.equals(obj); 64 } 65}

参考URL

【Android】ViewPagerを使ってListViewをカスタマイズ!ボタンがニョッキっと出てくるListViewを作る。iOS風?【VIewPager】

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問