AndroidStudioで、LinearLayoutをクリックして画面遷移する方法を教えてください!
解決したいこと
現在、GoogleMapを使ったスマホアプリをJavaで開発しています。
GoogleMap内のマーカーをクリックしたときに、クリックした場所の詳細ウインドウを表示させる機能を作りました。
そして次に、その詳細ウインドウをクリックしたら第二画面に遷移する機能を作ろうと試みたのですが、中々上手くいかないので解決法をご教授願います。
開発環境
Android Studio Dolphin | 2021.3.1 Patch 1
該当するソースコード
問題の箇所に☆マークつけました。
やりたいことは、idがwindowのLinearLayoutをクリックしたら第二画面に遷移するようにしたいです。
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Marker marker_1;
mMap.setOnMyLocationButtonClickListener(this);
mMap.setOnMyLocationClickListener(this);
enableMyLocation();
//店のデータをリストに格納
shopData shop = new shopData();
Map<Integer, shopDataModel> shopDataMap = shop.storage();
//リストの店を全て地図にピン刺し
for(Integer i = 0; i < shopDataMap.size(); i++){
shopDataModel shopModel = shopDataMap.get(i + 1);
LatLng place = new LatLng(shopModel.getLatitude(), shopModel.getLongitude());
mark = mMap.addMarker(new MarkerOptions().position(place));
mark.setTag(i);
}
// タップ時のイベントハンドラ登録
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
//マーカーの店データ取得
shopDataModel shopModel = shopDataMap.get((Integer)marker.getTag() + 1);
//店の名前
TextView shopName = findViewById(R.id.shopName);
shopName.setText(shopModel.getShopName());
//お店の写真
ImageView shopPic = findViewById(R.id.shopIcon);
int picId = getResources().getIdentifier(shopModel.getShopPic(), "drawable", getPackageName());
Log.i("MapsActivity", shopModel.getShopName() + "のとき、変数picIdの値は、" + picId + "です");
shopPic.setImageResource(picId);
//お店の詳細を表示
LinearLayout view = findViewById(R.id.window);
TranslateAnimation ta = new TranslateAnimation(0, 0, 0, -650);
ta.setDuration(500);
ta.setFillAfter(true);
view.startAnimation(ta);
//☆☆お店詳細ウィンドウを最前面に配置
View shopWindow = (LinearLayout)findViewById(R.id.window);
shopWindow.bringToFront();
//☆☆LinearLayoutクリックイベント
LinearLayout linear = (LinearLayout)findViewById(R.id.window);
linear.setOnClickListener(new WindowClickListener());
return false;
}
});
//ズームボタン設置
mMap.getUiSettings().setZoomControlsEnabled(true);
}
//☆☆クリックリスナー
public class WindowClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
Log.i("MapsActivity", "第二画面起動");
Intent intent = new Intent(MapsActivity.this, DetailActivity.class);
startActivity(intent);
}
}
第一画面レイアウト
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/bottom_navi_menu" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity"
tools:layout="@layout/activity_maps" />
☆☆ <LinearLayout
android:id="@+id/window"
android:layout_width="350dp"
android:layout_height="200dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="-150dp"
android:clickable="true">
<TextView
android:id="@+id/shopName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:textColor="#000000">
</TextView>
<ImageView
android:id="@+id/shopIcon"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp">
</ImageView>
<TextView
android:id="@+id/shopStation"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<Button
android:id="@+id/follow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:text="@string/follow_before"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="right">
</Button>
</LinearLayout>
</FrameLayout>
第二画面用ソースコード
public class DetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Intent intent = getIntent();
String shopName = intent.getStringExtra("shopName");
TextView lyShopName = findViewById(R.id.shopName_detail);
lyShopName.setText(shopName);
//アクションバー取得
ActionBar actionBar = getActionBar();
//戻るボタン有効化
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
第二画面レイアウト
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_detail"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/shopName_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
</TextView>
</androidx.constraintlayout.widget.ConstraintLayout>
自分で試したこと
↓下記のサイトを見て、レイアウトに「android:clickable=”true”」を付けましたが、クリックしても反応せず。。。
http://log.miraoto.com/2012/02/300/
また、onClick関数の中でログ出力処理を書きましたが、ログは表示されなかったのでonClick関数は通っていないようです・・・。
0 likes